Home > Net >  Stop Unity from moving diagonally using transform translate
Stop Unity from moving diagonally using transform translate

Time:11-14

I have a game in which the character can only move horizontally and vertically, not diagonally.

I have tried to implement this with this code, which should check if the player is pressing down/up or left/right:

if (Input.GetKey("down"))
{
    moveHor = false;
    moveVer = true;
    if(moveVer == true)
    {
        transform.Translate(Vector3.down * Time.deltaTime * speed);
    }
}
if (Input.GetKey("up"))
{
    moveHor = false;
    moveVer = true;
    if (moveVer == true)
    {
        transform.Translate(Vector3.up * Time.deltaTime * speed);
    }
}
if (Input.GetKey("right"))
{
    moveVer = false;
    moveHor = true;
    if(moveHor == true)
    {
        transform.Translate(Vector3.right * Time.deltaTime * speed);
    }
}
if (Input.GetKey("left"))
{
    moveVer = false;
    moveHor = true;
    if (moveHor == true)
    {
        transform.Translate(Vector3.left * Time.deltaTime * speed);
    }
}

This code is ran in the Update() method.

The thing is, it's not working and I can't figure out why.

I hope someone knows what to do.

CodePudding user response:

Try to use else-if to translate only in one direction per update cycle. I've also removed nested if statements from your code, which were redundant and unneeded.

if (Input.GetKey("down"))
{
    moveHor = false;
    moveVer = true;

    transform.Translate(Vector3.down * Time.deltaTime * speed);
}
else if (Input.GetKey("up"))
{
    moveHor = false;
    moveVer = true;

    transform.Translate(Vector3.up * Time.deltaTime * speed);
}
else if (Input.GetKey("right"))
{
    moveVer = false;
    moveHor = true;

    transform.Translate(Vector3.right * Time.deltaTime * speed);
}
else if (Input.GetKey("left"))
{
    moveVer = false;
    moveHor = true;

    transform.Translate(Vector3.left * Time.deltaTime * speed);
}

CodePudding user response:

In situations like this, you can read all of the keys, and have them zero each other out. This means that when two keys are pressed (e.g. the left AND the right), the character will stop moving horizontally.

We also take a look at the order of processing as well, so we're not performing unnecessary calculations. We do that by performing the Vector2/3 calculations as late in the chain as possible, as there's usually 2 or 3 parts to these structs.

What we're left with is:

private void Update ( )
{
    var movement = Vector3.zero;
    if ( Input.GetKey ( "left" ) )
        movement.x -= -1;
    if ( Input.GetKey ( "right" ) )
        movement.x  = 1;
    if ( Input.GetKey ( "down" ) )
        movement.y -= 1;
    if ( Input.GetKey ( "up" ) )
        movement.y  = 1;

    // If both movement.x and movement.y are not equal to zero, zero out the horizontal, leaving the vertical.
    if ( movement.x != 0 && movement.y != 0 )
        movement.y = 0;

    // multiply the vector last for performance.
    transform.Translate ( speed * Time.deltaTime * movement );
}
  • Related