首页 > 解决方案 > 如何为统一 2D 角色控制器添加跳跃延迟?

问题描述

这是我的字符控制器代码。

    public float speed;
float Velocity;
public float jump;

void Update () 
{

    Velocity = 0;

    if (Input.GetKey (KeyCode.LeftArrow) || Input.GetKey (KeyCode.A)) 
    {
        Velocity = -speed;
    }
    if (Input.GetKey (KeyCode.RightArrow) || Input.GetKey (KeyCode.D)) 
    {
        Velocity = speed;
    }

    if (Input.GetKey (KeyCode.UpArrow) || Input.GetKey("space") || Input.GetKey (KeyCode.W))
    {
        GetComponent<Rigidbody2D> ().velocity = new Vector2 (GetComponent<Rigidbody2D> ().velocity.x, 
jump);
        }

        GetComponent<Rigidbody2D> ().velocity = new Vector2 (moveVelocity, GetComponent<Rigidbody2D> 
().velocity.y);

    }
}

这是一个非常简单的运动代码,这是我从 Brackeys 获得的凸轮跟随代码 我的代码存在无限跳转问题,我不知道如何修复它我不知道正确的语法来阻止它。我正在使用 Unity 2019.4.2f1 和 VScode 2020。我不确定添加我使用的版本是否会有所帮助,但我总是收到编译器错误,说当前上下文中不存在这些东西,但我不知道其他任何东西确实存在于将起作用的当前上下文。

    void Update()
{
    transform.position = playerPos.position + offset;
}

标签: unity3d

解决方案


要修复无限跳跃,请使用Input.GetKeyDown(KeyCode.UpArrow)代替Input.GetKey(KeyCode.UpArrow). GetKey 函数用于检查何时按下某个键。GetKeyDown 函数用于获取第一次按下键的时间。


推荐阅读