如何在unity3d中为我的动作脚本添加双跳



我试图学习编码,并观看youtube教程学习编码,我在原型游戏中发现了这段代码,我只是想问我应该添加什么来让我的角色做双跳?

代码:

public class fpsmovement : MonoBehaviour
{

private float yaw = 0.0f, pitch = 0.0f;
private Rigidbody rb;
[SerializeField] float movementspeed = 5.0f, sensitivity = 2.0f;
// Start is called before the first frame update
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
rb = gameObject.GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
if (Input.GetKey(KeyCode.Space) && Physics.Raycast(rb.transform.position, Vector3.down, 1 + 0.001f))
rb.velocity = new Vector3(rb.velocity.x, 5.0f, rb.velocity.z);
Look(); 
}
void FixedUpdate()
{
Movement();  
}
void Look()
{
pitch -= Input.GetAxisRaw("Mouse Y") * sensitivity;
pitch = Mathf.Clamp(pitch, -90.0f, 90.0f);
yaw += Input.GetAxisRaw("Mouse X") * sensitivity;
Camera.main.transform.localRotation = Quaternion.Euler(pitch, yaw, 0);
}
void Movement()
{
Vector2 axis = new Vector2(Input.GetAxis("Vertical"), Input.GetAxis("Horizontal")).normalized * movementspeed;
Vector3 forward = new Vector3(-Camera.main.transform.right.z, 0.0f, Camera.main.transform.right.x);
Vector3 wishDirector = (forward * axis.x + Camera.main.transform.right * axis.y + Vector3.up * rb.velocity.y);
rb.velocity = wishDirector;
}

}

首先,你需要检查你的玩家是否被禁足才能跳跃。你可能有一个球员可以像火箭一样永远再次跳跃。然后你需要创建一个布尔变量来检查玩家是否可以双击。当您跳跃时将变量设置为true,当您进行第二次跳跃时将该变量设置为false。此外,为了允许跳跃,你应该检查球员是否接地,或者是否可以双跳。youtube上有很多关于这个主题的教程,我希望你能轻松找到所需的代码。

您可以尝试两种方法,"跳转"以及";Jump1";,第一个";跳转"方法首先使用if来判断它是否在地上,如果允许它在地上跳跃;Jump1";该方法判断它是否不在地面上。在此基础上,实现了判断跳转状态的代码逻辑,并最终重置按钮。

最新更新