繁体   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