简体   繁体   English

如何确保在 C# 中没有按下按钮?

[英]How do I make sure that a button is not pressed down in C#?

So I'm currently making a game in C# and I've added the ability sprint by pressing and holding left shift but the problem is that I don't want the player to be able to sprint while crouching which can happen at the moment so long as the are holding down both left shift and left control.所以我目前正在 C# 中制作游戏,我通过按住左移添加了能力冲刺,但问题是我不希望玩家能够在蹲下时冲刺,这可能发生在此刻所以只要同时按住左移和左控制。 I've tried 'GetButtonUp' and 'GetButtonDown' but they don't work at all.我试过“GetButtonUp”和“GetButtonDown”,但它们根本不起作用。 Here is my current sprint script with the issue:这是我当前遇到问题的 sprint 脚本:

public class AdvancedMovement : MonoBehaviour
{
    //Variables;
    PlayerMovement basicMovementScript;
    public float speedBoost = 10f;

    // Start is called before the first frame update
    void Start()
    {
        basicMovementScript = GetComponent<PlayerMovement>();
    }

    // Update is called once per frame
    void Update()
    {

        if (Input.GetKeyDown(KeyCode.LeftShift))
            beginSprint();
        else if (Input.GetKeyUp(KeyCode.LeftShift))
            endSprint();
    }

    private void beginSprint()
    {
        basicMovementScript.speed += speedBoost;
    }

    private void endSprint()
    {
        basicMovementScript.speed -= speedBoost;
    }
}

I would greatly appreciate some help.我将不胜感激一些帮助。 Thanks.谢谢。

Input.GetKeyDown and Input.GetKeyUp are only called once, and that's when the action is made and the next frame is rendered. Input.GetKeyDownInput.GetKeyUp仅被调用一次,即在执行操作并呈现下一帧时。

If by any misfortune that specific frame is skipped, either for performance or debugging reasons, they could lead to unexpected behavior.如果由于性能或调试原因而跳过特定帧的任何不幸,它们可能会导致意外行为。 which in your case could possibly translate into endSprint being called without the Input.GetKeyDown(KeyCode.LeftShift) being detected as an example.在您的情况下,这可能会转化为endSprint被调用,而不会检测到Input.GetKeyDown(KeyCode.LeftShift)作为示例。

What I suggest is that you use Input.GetKey instead, which is called at every update, so long as the Key is held down.我建议您改用Input.GetKey ,只要按住键,每次更新都会调用它。

In your case, you could do:在你的情况下,你可以这样做:

if (Input.GetKeyDown(KeyCode.LeftShift) &&.Input.GetKey(KeyCode.Leftcontrol)) which will make sure the Left Ctrl Key is not held down. if (Input.GetKeyDown(KeyCode.LeftShift) &&.Input.GetKey(KeyCode.Leftcontrol))这将确保不按住左 Ctrl 键。

What I would also do in a similar situation, instead of using Input.GetKeyDown(KeyCode.LeftShift) I would create a flag _isSprinting and use it in a way similar to the following:在类似情况下我也会做的,而不是使用Input.GetKeyDown(KeyCode.LeftShift)我会创建一个标志_isSprinting并以类似于以下方式使用它:

void Update()
{

    if (!_isSprinting && Input.GetKey(KeyCode.LeftShift) && !Input.GetKey(KeyCode.Leftcontrol)))
    {
        beginSprint();

        _isSprinting = true;
    }

    if (_isSprinting && !Input.GetKey(KeyCode.LeftShift))
    {
        endSprint();

        _isSprinting = false;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM