簡體   English   中英

unity 使用transform.position 方法停止對角線移動

[英]Unity stop diagonal movement using transform.position method

我在嘗試理解如何阻止我的 Ball 對象在向左或向右滑動時對角移動時遇到了很多麻煩,基本上發生的情況是:我向一個方向滑動一個球,然后球從頂部落下屏幕。 當我滑動球時,它會左右移動,但由於向下移動,它也會略微下降 - 我該如何更改? 我的游戲是二維的

這是您應該需要的所有代碼

//Variables 
public float ballSpeed = 10; //This will handle our Balls left and Right movement when swiped
public float fallSpeed = 2;  //This will handle the speed at which our ball falls
[HideInInspector]
public bool hitWall = false; //Check if our ball has collided with a wall
public bool moveRight, moveLeft;

public RoundHandler roundHandler;


private void OnEnable()
{
    //Get our Components 
    roundHandler = FindObjectOfType<RoundHandler>();
}

#region functions 

void checkWhereToMove()
{
    if (moveLeft == true)
    {
        transform.position -= transform.right * Time.deltaTime * ballSpeed;

    }

    if (moveRight == true)
    {
        transform.position += transform.right * Time.deltaTime * ballSpeed;

    }
}

public void moveDown() {

    //Set our Fall Speed modified by our Current rounds
    fallSpeed = roundHandler.ballFallSpeed;

    if (hitWall != true) {
        //Check if we arnt moving left or Right so that we can move down
        if (moveLeft == false || moveRight == false)
        {
            //Move our Ball down 
            transform.position -= transform.up * Time.deltaTime * fallSpeed;
            //Get our movement input
            checkWhereToMove();
        }    
    } 
}
#endregion

private void FixedUpdate()
{
    moveDown();
}
  1. 測試運動需要檢查兩個方向是否都是錯誤的。

  2. checkWhereToMove() 在錯誤的地方。

  3. 向下移動后,兩個方向都需要重置為false。

public void moveDown() {
        //Set our Fall Speed modified by our Current rounds
        fallSpeed = roundHandler.ballFallSpeed;

        //Get our movement input
        checkWhereToMove();

        if (hitWall != true) {
            //Check if we arnt moving left or Right so that we can move down

            if (moveLeft == false && moveRight == false) {
                //Move our Ball down
                transform.position -= transform.up * Time.deltaTime * fallSpeed;

                //Reset left and right movement
                moveLeft = false;
                moveRight = false;
            }
        }
    }

暫無
暫無

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

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