gpt4 book ai didi

unity-game-engine - Unity3D中如何平滑跳转

转载 作者:行者123 更新时间:2023-12-02 20:58:24 25 4
gpt4 key购买 nike

我给Player添加了一个CharacterController,但是当我测试跳跃功能时,我发现Player会立即向上移动。

    if (Player.isGrounded) 
{
if (jump)
{
Move.y = JumpSpeed;
jump = false;
Player.Move (Move * Time.deltaTime);
}
}
Move += Physics.gravity * Time.deltaTime * 4f;
Player.Move (Move * Time.fixedDeltaTime);`

最佳答案

  1. 您在一帧中调用 Player.Move() 两次。这可能是一个问题。
  2. 您正在向Move矢量添加重力,这意味着当您调用此代码时它始终会向上。
  3. Move这样命名变量并不是一个好的约定。它在阅读时会造成困惑,因为已经有一个同名的方法。将其更改为 moveDirection

这里是示例代码:

public class ExampleClass : MonoBehaviour {
public float speed = 6.0F;
public float jumpSpeed = 8.0F;
public float gravity = 20.0F;
private Vector3 moveDirection = Vector3.zero;
CharacterController controller;
void Start()
{
controller = GetComponent<CharacterController>();
}

void Update() {
if (controller.isGrounded) {
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton("Jump"))
moveDirection.y = jumpSpeed;

}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
}

希望这有帮助。

关于unity-game-engine - Unity3D中如何平滑跳转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39481422/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com