如何为统一的2D角色控制器添加跳跃延迟?

时间:2020-11-09 13:56:23

标签: unity3d

这是我的角色控制器代码。

    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获得的cam跟随代码 我的代码存在无限跳转问题,我不知道该如何解决 我尚不知道要停止使用的正确语法。 我正在使用Unity 2019.4.2f1和VScode 2020。 我不确定是否添加我使用的版本会有所帮助 但是我总是收到编译器错误,说当前环境中不存在任何东西,但是我不知道在当前环境中仍然有效的其他任何东西。

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

1 个答案:

答案 0 :(得分:0)

要解决无限跳转,请使用Input.GetKeyDown(KeyCode.UpArrow)而不是Input.GetKey(KeyCode.UpArrow)。 GetKey函数用于检查按下键的时间。 GetKeyDown函数用于第一次获取按键。