【问题标题】:Unity Keyboard Movement Easing?Unity 键盘移动缓动?
【发布时间】:2021-11-23 05:55:32
【问题描述】:

我正在尝试复制编辑器的相机以供运行时使用,并且一切都按预期工作,但我正在尝试缓和运动并且无法使其正常工作。这是我的运动代码。它工作正常,但没有像我想要的那样缓和。

             Vector3 move = Vector3.zero;

             if (Input.GetKey(KeyCode.W))
                 move += Vector3.forward * currentSpeed;
             if (Input.GetKey(KeyCode.S))
                 move -= Vector3.forward * currentSpeed;
             if (Input.GetKey(KeyCode.D))
                 move += Vector3.right * currentSpeed;
             if (Input.GetKey(KeyCode.A))
                 move -= Vector3.right * currentSpeed;
             if (Input.GetKey(KeyCode.E))
                 move += Vector3.up * currentSpeed;
             if (Input.GetKey(KeyCode.Q))
                 move -= Vector3.up * currentSpeed;

             transform.Translate(move);

我尝试了一个目标 Vector3,将其设置为 move,然后将位置设置为 destination。这确实像预期的那样轻松,但是方向都被打破了。左移前移,右移后移,依此类推。

我已尝试迁移到 fixedUpdate,但仍然没有成功。

任何帮助将不胜感激!

【问题讨论】:

    标签: unity3d input camera controls


    【解决方案1】:

    经过大量实验,可以通过在更新循环中执行以下操作来实现缓动/平滑:

    float x, y, z;
    x = Input.GetAxis("Horizontal") * currentSpeed * Time.deltaTime;
    z = Input.GetAxis("Vertical") * currentSpeed * Time.deltaTime;
    y = 0;
    transform.Translate(x,y,z);
    

    【讨论】:

      【解决方案2】:

      尝试使用 CharacterController。它是 Unity 的组件。它易于使用且流畅。

      public CharacterController controler;
      
      public void Start(){
      controller = GetComponent<CharacterController>();}
      
      public void Move(){
      controller.Move(moveDirection * Time.deltaTime);}
      

      在 youtube 上,您可以找到 CharacterController 的教程。

      【讨论】:

      • 我确实看过它,但因为它本身不是一个角色,实际上只是我试图移动的相机,所以我认为它不合适。我真的只是在寻找一种方法来平滑我已经拥有的代码,因为它现在可以完美运行(平滑除外)
      • 然后试试这个。这是简单的二维运动,但您也可以在相机上使用它。 public class CameraController : MonoBehaviour { private float moveSpeed = 0.5f; void Update () { if (Input.GetAxisRaw("Horizontal") != 0 || Input.GetAxisRaw("Vertical") != 0) { transform.position += moveSpeed * new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical")); }
      • 感谢您的贡献,但基于我提供的现有代码,我不确定它如何适用。此外,似乎没有应用任何平滑,这是我需要帮助的。无论如何,感谢您的宝贵时间!
      • 我意识到正确的词是“缓和”,而不是平滑。对于任何混淆,我深表歉意。
      猜你喜欢
      • 1970-01-01
      • 2017-03-24
      • 1970-01-01
      • 1970-01-01
      • 2019-05-19
      • 1970-01-01
      • 2018-06-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多