【问题标题】:Move object forward in facing direction朝正面方向向前移动对象
【发布时间】:2017-06-09 14:32:01
【问题描述】:

基本上,我试图让我的角色在相机所面对的方向上向前冲刺一段特定的距离。这是一个 fps,我能做到的最好的脸是在全局 X 轴上向前冲,这显然是错误的。

我有这个从更新函数调用的函数:

Debug.Log("We are dashing.");

    if (!inputDash || !canControl)
        dashing.lastButtonDownTime = -100;

    if (inputDash && dashing.lastButtonDownTime < 0 && canControl)
        dashing.lastButtonDownTime = Time.time;

    if (dashing.enabled && canControl && (Time.time - dashing.lastButtonDownTime < 0.2))
    {            
        dashing.dashing = true;
        dashing.lastButtonDownTime = -100;
        dashing.dashCounter ++;

        dashing.dashDir = Camera.mainCamera.transform.position;

        //velocity = Vector3.MoveTowards(dashing.dashDir, (dashing.dashDir + Vector3(dashing.dashDir.x + 2, 0, 0)), (2 * Time.deltaTime));

        Debug.Log("We just dashed.");            
    }       

    return velocity;

我为 dashing.dashDir 尝试了许多不同的方法,但都没有奏效。我知道我基本上是想在相机的局部轴上冲刺?

我也试过这个:

dashing.dashDir = Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));            
velocity += dashing.dashDir * dashing.baseDistance;

任何帮助将不胜感激,因为这让我发疯了。

最后一点。我希望将玩家朝正面方向快速向前冲刺大约 3m,然后在冲刺完成时加速。抱歉,如果不清楚。

【问题讨论】:

    标签: unity3d unityscript


    【解决方案1】:

    这是我的答案,它运行良好:向前移动、旋转并与其他对象碰撞(具有 RigidBody 和 Box/Capsule Collider)。 t这是基于 Burkhard 的回答。

    但在此之前:创建一个空对象,将其设置为播放器的子对象,然后将相机对象拖到空对象中。

    注意:您可以将摄像头放在播放器后面以获得最佳视野。

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
      public class CubeControl : MonoBehaviour {
    
        public float speed = 10.0f;
        Rigidbody rb;
        GameObject playerEmpty;
        // Use this for initialization
        void Start () {
    
            rb = GetComponent<Rigidbody> ();
    
        }
    
        // Update is called once per frame
        void Update () {
    
            float Haxis = Input.GetAxis ("Horizontal");
            float Vaxis = Input.GetAxis ("Vertical");
    
    
            //Go Forward
            if(Input.GetKeyDown(KeyCode.UpArrow))
                {
    
                    //Works perfectly but does no longer collide
                    //rb.transform.position += transform.forward * Time.deltaTime * 10.0f;
                   //Not moving in the right direction
                    //rb.velocity += Vector3.forward * 5.0f;
    
    
                  rb.velocity += Camera.main.transform.forward * speed;
    
                //rb.rotation() += new Vector3 (0.0f, headingAngle, 0.0f) * 5.0f;
                //rb.velocity += gameObject.transform.localEulerAngles * Time.deltaTime * 10.0f ; 
                }
            if(Input.GetKeyDown(KeyCode.DownArrow))
                {
                    rb.velocity -= Camera.main.transform.forward * speed;
                }    
                Vector3 rotationAmount = Vector3.Lerp(Vector3.zero, new Vector3(0f, 10.0f * (Haxis < 0f ? -1f : 1f), 0f), Mathf.Abs(Haxis));
                Quaternion deltaRotation = Quaternion.Euler(rotationAmount * Time.deltaTime);
                this.transform.rotation = (this.transform.rotation * deltaRotation);
    
        }
    }
    

    【讨论】:

      【解决方案2】:

      要沿局部轴在 a 中移动对象,您需要像这样使用该对象变换

      dir = Camera.mainCamera.transform.forward;
      

      如果你还想增加你当前的速度,你需要像这样使用它

      newVelocity = Camera.mainCamera.transform.forward * (baseSpeed + currectVelocity.magnitude);
      

      这将设置您的相机前进的方向并将 baseSpeed 添加到您当前的速度。

      如果你想设置距离而不是速度,你可以这样计算

      speed = distance / time;
      

      这是您想要行驶的距离以及破折号的持续时间。如果你这样做,我不会将它添加到当前速度,因为这会改变行进的距离。

      【讨论】:

        最近更新 更多