如何在unity / c#中使一个命令“替代”另一个命令?

时间:2019-01-03 19:03:29

标签: c# unity3d

我正在使用GetKey在Unity3D中制作一个非常简单的移动器。 我知道这听起来很简单,但是我是一个初学者。 下面是代码。我可以看到问题,但是即使我尝试了很多解决方法,也找不到解决的方法。 预先谢谢你!

我尝试将代码从GetKey更改为GetKeyDown,但这没有用。

void Update()
{
    bool w, a, s, d;
    w = Input.GetKey(KeyCode.W);
    a = Input.GetKey(KeyCode.A);
    s = Input.GetKey(KeyCode.S);
    d = Input.GetKey(KeyCode.D);

    if (w)
    {
        transform.Translate(0.0f, speed * Time.deltaTime, 0.0f);
    }
    if (a)
    {
        transform.Translate(-speed * Time.deltaTime, 0.0f, 0.0f);
    }
    if (s)
    {
        transform.Translate(0.0f, -speed * Time.deltaTime, 0.0f);
    }
    if (d)
    {
        transform.Translate(speed * Time.deltaTime, 0.0f, 0.0f);

    }
}

预计如果我先按“ A”然后再按“ D”,那么移动器将向右移动(就像我只按D一样,因为我最后一次按了D)。 目前,该机芯已取消并停留在原处。

2 个答案:

答案 0 :(得分:0)

现在,我得到了您想要的东西:按下一个新按钮应取消前一个按钮

您可以使用协程:

private void Update()
{
    bool w, a, s, d;
    w = Input.GetKeyDown(KeyCode.W);
    a = Input.GetKeyDown(KeyCode.A);
    s = Input.GetKeyDown(KeyCode.S);
    d = Input.GetKeyDown(KeyCode.D);

    if (w)
    {
        StopAllCoroutines();
        StartCoroutine (HandleKey(KeyCode.W));
    }
    else if (a)
    {
        StopAllCoroutines();
        StartCoroutine (HandleKey(KeyCode.A));
    }
    else if (s)
    {
        StopAllCoroutines();
        StartCoroutine (HandleKey(KeyCode.S));
    }
    else if (d)
    {
        StopAllCoroutines();
        StartCoroutine (HandleKey(KeyCode.D));
    }
}

private IEnumerator HandeKey(KeyCode code)
{
    Vector3 vector;

    Switch(code)
    {
        case KeyCode.W:
            vector = new Vevtor3(0.0f, speed * Time.deltaTime, 0.0f);
            break;


        case KeyCode.A:
            vector = new Vector3(-speed * Time.deltaTime, 0.0f, 0.0f);

            break;

        case KeyCode.D:
            vector = new Vector3(speed * Time.deltaTime, 0.0f, 0.0f);
            break;

        case KeyCode.S:
            vector = new Vector3(0.0f, -speed * Time.deltaTime, 0.0f);
            break;
    }

    // Go on while the button stays pressed
    while(Input.GetKey(code))
    {
        transform.Translate(vector);
        yield return null;
    }
}

或者在没有例程的情况下做同样的事情:

private KeyCode lastKeyPressed;
private Vector3 vector;

private void Update ()
{
    bool w, a, s, d;
    w = Input.GetKeyDown(KeyCode.W);
    a = Input.GetKeyDown(KeyCode.A);
    s = Input.GetKeyDown(KeyCode.S);
    d = Input.GetKeyDown(KeyCode.D);  

    if (w)
    {
        lastKeyPressed = KeyCode.W;
        vector = new Vector3(0.0f, speed * Time.deltaTime, 0.0f);
    }
    else if (a)
    {
        lastKeyPressed = KeyCode.A;
        vector = new Vector3(-speed * Time.deltaTime, 0.0f, 0.0f);
    }
    else if (s)
    {
        lastKeyPressed = KeyCode.S;
        vector = new Vector3(0.0f, -speed * Time.deltaTime, 0.0f);
    }
    else if (d)
    {
        lastKeyPressed = KeyCode.D;
        vector = new Vector3(speed * Time.deltaTime, 0.0f, 0.0f);
    }

    if(Input.GetKey(lastKeyPressed))
    {
        transform.Translate(vector);
    }
}

但是我不太喜欢^^

答案 1 :(得分:0)

您可以在对象中定义变量,以跟踪首先按下哪个键。在这种情况下,仅当按下 键而不按下另一个键时,我们才将这些值翻转为true。

然后,我们仅在不按住另一个方向或首先按下另一个方向的情况下才允许在一个方向上移动。

private bool WPressedFirst = false;
private bool SPressedFirst = false;
private bool APressedFirst = false;
private bool DPressedFirst = false;

void Update()
{
    bool w, a, s, d;
    w = Input.GetKey(KeyCode.W);
    a = Input.GetKey(KeyCode.A);
    s = Input.GetKey(KeyCode.S);
    d = Input.GetKey(KeyCode.D);

    // if any key isn't being pressed, set its pressedfirst value to false
    if(!w)
    {
        WPressedFirst = false;
    }
    if(!a)
    {
        APressedFirst = false;
    }
    if(!s)
    {
        SPressedFirst = false;
    }
    if(!d)
    {
        DPressedFirst = false;
    }

    // if a key is being pressed AND the other one isn't, set it to pressedfirst true
    if(w && !SPressedFirst)
    {
        WPressedFirst = true;
    }
    if(s && !WPressedFirst)
    {
        SPressedFirst = true;
    }
    if(a && !DPressedFirst)
    {
        APressedFirst = true;
    }
    if(d && !APressedFirst)
    {
        DPressedFirst = true;
    }

    // only allow movement in a direction if it is pressed and the other isn't pressed
    // OR if it is pressed and the other was pressed first.
    if (w && (!s || SPressedFirst))
    {
        transform.Translate(0.0f, speed * Time.deltaTime, 0.0f);
    }
    if (a && (!d || DPressedFirst))
    {
        transform.Translate(-speed * Time.deltaTime, 0.0f, 0.0f);
    }
    if (s && (!w || WPressedFirst))
    {
        transform.Translate(0.0f, -speed * Time.deltaTime, 0.0f);
    }
    if (d && (!a || APressedFirst))
    {
        transform.Translate(speed * Time.deltaTime, 0.0f, 0.0f);

    }
}