简体   繁体   English

unity 使用transform.position 方法停止对角线移动

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

I'm having a ton of trouble trying to understand how to stop my Ball objects from moving diagonally when they are swiped left or right, essentially what happens is: I swipe a ball in a direction, and the ball falls from the top of the screen.我在尝试理解如何阻止我的 Ball 对象在向左或向右滑动时对角移动时遇到了很多麻烦,基本上发生的情况是:我向一个方向滑动一个球,然后球从顶部落下屏幕。 When i swipe the Ball it moves left and right but it also falls slightly still because of the downwards movement - how do i change this ?当我滑动球时,它会左右移动,但由于向下移动,它也会略微下降 - 我该如何更改? My game is 2D我的游戏是二维的

Here is all the code you should need这是您应该需要的所有代码

//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. Testing for movement requires checking to see if both directions are false.测试运动需要检查两个方向是否都是错误的。

  2. checkWhereToMove() is in the wrong place. checkWhereToMove() 在错误的地方。

  3. After moving down, both directions need to be reset to false.向下移动后,两个方向都需要重置为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