簡體   English   中英

Unity2D 中的隨機錯誤

[英]Random Bug in Unity2D

我最近一直在為 GMTK 2022 Game Jam 做一個項目,我遇到了一個非常奇怪的問題。 當您移動並按空格鍵時,我有一個破折號。 它使您朝着您的速度方向移動,然后在短時間內讓您快速移動。 它在所有情況下都可以正常工作,除非您移動的方向是向上和向左,在這種情況下,if 語句奇怪地不會觸發。 我確信這很愚蠢,但我在過去的一個小時里一直在排除故障,這讓我發瘋了。

// Update is called once per frame
void Update()
{
    playerInputh = 0;
    playerInputv = 0;
    if (Input.GetKey("right"))
    {
        playerInputh = 1;
    }
    if (Input.GetKey("left"))
    {
        playerInputh = -1;
    }
    if (Input.GetKey("right") && Input.GetKey("left"))
    {
        playerInputh = 0;
    }
    if (Input.GetKey("up"))
    {
        playerInputv = 1;
    }
    if (Input.GetKey("down"))
    {
        playerInputv = -1;
    }
    if (Input.GetKey("up") && Input.GetKey("down"))
    {
        playerInputv = 0;
    }

    Vector2 screenPosition = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
    Vector2 mouseWorldPosition = Camera.main.ScreenToWorldPoint(screenPosition);


    //This is the dash that isn't working:
    if ((Input.GetKeyDown(/*"right shift"*/"space")) && (playerInputh != 0 || playerInputv != 0))
    {
        Debug.Log("Dash");
        //Vector2 transform2dposition = new Vector2(transform.position.x, transform.position.y);
        m_Rigidbody.AddForce((m_Rigidbody.velocity) * 500f);
        wJumpTimer = airControlAfterJump;
        speed = maxSpeed*3.5f;
        StartCoroutine(Roll());
    }


}

void FixedUpdate()
{
    //no moving while jumping!!!
    if (wJumpTimer > 0)
    {
        wJumpTimer -= 1;
    }
    else
    {
        wJumpTimer = 0;
    }

    //move
    if (playerInputh != 0 && playerInputv != 0) //make diagonals no super sayan
    {
        playerInputh *= moveLimiter;
        playerInputv *= moveLimiter;

        one_h = playerInputh;//futureproof
        one_v = playerInputv;
    }
    if ((playerInputh != 0 || playerInputv != 0) && speed < maxSpeed) //are we hitting the move buttons??
    {
        speed += acceleration;//accelerate
        one_h = playerInputh;//futureproof
        one_v = playerInputv;

    }
    else
    {     
        if (speed > 0f) //are we getting off the ride
        {
            speed -= deceleration; //decelerate
        }
        else
        {
            speed = 0f; //no funny buisness
        }
    }

    m_Rigidbody.velocity = new Vector2(one_h * speed, one_v * speed); //actually move





}


void SetFace(int diceNumb)
{
    rndr.sprite = sprites[diceNumb];
}
IEnumerator Roll()
{
    Random diceNumb = new Random();
    rndr.sprite = sprites[diceNumb.Next(0,5)];
    yield return new WaitForSeconds(0.125f);
    rndr.sprite = sprites[diceNumb.Next(0, 5)];
    yield return new WaitForSeconds(0.125f);
    rndr.sprite = sprites[diceNumb.Next(0, 5)];
    yield return new WaitForSeconds(0.125f);
    rndr.sprite = sprites[diceNumb.Next(0, 5)];
    yield return new WaitForSeconds(0.125f);
    var newValue = diceNumb.Next(0, 5);
    FaceValue = newValue + 1;
    rndr.sprite = sprites[newValue];

}

你的輸入是如何設置的? 我懷疑你有一個鍵綁定到多個動作,比如一個鍵被設置為一個動作的主鍵,而另一個鍵被設置為另一個動作。

就個人而言,我也會停止使用賦值運算符並改為增量。 代替

    playerInputh = 0;
    if (Input.GetKey("right"))
    {
        playerInputh = 1;
    }
    if (Input.GetKey("left"))
    {
        playerInputh = -1;
    }
    if (Input.GetKey("right") && Input.GetKey("left"))
    {
        playerInputh = 0;
    }

你可以做

    playerInputv = 0;
    if (Input.GetKey("right"))
    {
        playerInputh += 1;
    }
    if (Input.GetKey("left"))
    {
        playerInputh += -1;
    }

此處的最終結果是相同的 - 如果您按下兩個鍵,結果總和為零,但代碼更易於閱讀(IMO)。

當您檢查鍵綁定的內容時,還要檢查空格的替代項,因為這是您需要沖刺的另一個觸發器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM