平滑的Lerp运动?

d d*_*doe 7 c# unity-game-engine

目前,我的脚本是将玩家移动到场景周围.我怎样才能顺利移动?

void FixedUpdate()
{
    bool running = Input.GetKey(KeyCode.LeftShift);

    float h = Input.GetAxisRaw("Horizontal");
    float v = Input.GetAxisRaw("Vertical");

    bool isWalking = Mathf.Abs(h) + Mathf.Abs(v) > 0;

    movement = ((running) ? runSpeed : walkSpeed) * new Vector3(h, 0.0f, v).normalized;
    if (isWalking)
    {
        transform.position += movement * Time.fixedDeltaTime;
        transform.LookAt(transform.position + movement);
    }

}
Run Code Online (Sandbox Code Playgroud)

Igg*_*ggy 10

  1. 创建速度矢量:

    Vector3 velocity = Vector3.zero;

  2. 将您的运动矢量添加到力度:

    velocity += movement;

  3. 将速度添加到实际位置:

    transform.position += velocity;

  4. 通过随时间减少速度来平滑速度:

    velocity *= 0.975f;

  • 代码很简单,哪一部分没有意义? (3认同)