[SOLVED] 2D: Object's rotation are adding up?

Hi, I’ve set up an enemy that has a random movement and based on that, it’ll rotate based on the direction its going. So the default one is that if it’s moving up, it won’t rotate at all (0 degrees) and if it changes direction to the right, it’ll rotate 90 degrees, left is 270 degrees while down is 180 degrees. The problem with this code is that the rotation seems to add up when I just want it to set a new rotation. Let’s set up a little scenario here.

Enemy moves up first, so the rotation should be 0. Then it moves left, rotation is currently 270 degrees. But when it moves down for example, the rotation becomes 90 degrees (not 180 degrees) and it seems to be adding up to the previous rotation (270 + 180 = 450, 450 - 360 = 90).

Here’s the RandomMovement method:

void RandomMovement()
{
    numberGenerator = Random.Range(1, 13);

    if (numberGenerator >= 1 && numberGenerator <= 3)
    {
        enemy.velocity = transform.up * stats.speed;
        transform.eulerAngles = new Vector3(0, 0, 0); // Expected inspector to show rotation (0, 0, 0)
    }
    else if (numberGenerator >= 4 && numberGenerator <= 6)
    {
        enemy.velocity = -transform.up * stats.speed;
        transform.eulerAngles = new Vector3(0, 0, 180); // Expected inspector to show rotation (0, 0, 180)
    }
    else if (numberGenerator >= 7 && numberGenerator <= 9)
    {
        enemy.velocity = transform.right * stats.speed;
        transform.eulerAngles = new Vector3(0, 0, 90); // Expected inspector to show rotation (0, 0, 90)
    }
    else if (numberGenerator >= 10 && numberGenerator <= 12)
    {
        enemy.velocity = -transform.right * stats.speed;
        transform.eulerAngles = new Vector3(0, 0, 270); // Expected inspector to show rotation (0, 0, 270)
    }
}

I’ve tried a lot of possibility to fix it but none of them worked. So, if anyone knows how to fix it, that’ll be awesome. Again, I want it so that everytime the player changes direction, the rotation will be changed, just like how one would change it’s value in the inspector and not have it added up to previous rotation.

do you have any other code that affects the rotation? I’m not seeing anything “additive” about what you’ve pasted in. Is the transform being rotated a child of another gameobject (or just “what is enemy in relation to transform in your snippet”)?

So since you’re setting rotation to the cardinal directions, I’m assuming your player is always facing down the World Axes, correct?

If that is the case, you could do this kind of thing to set movement direction and rotation:
(disclaimer: untested, just for ideas)

void RandomMovement() {
    numberGenerator = Random.Range(1, 13);

    Vector3 movementDirection = Vector3.zero;

    if(numberGenerator >= 1 && numberGenerator <= 3) {
        movementDirection = Vector3.up;
    } else if(numberGenerator >= 4 && numberGenerator <= 6) {
        movementDirection = Vector3.down;
    } else if(numberGenerator >= 7 && numberGenerator <= 9) {
        movementDirection = Vector3.right;
    } else if(numberGenerator >= 10 && numberGenerator <= 12) {
        movementDirection = Vector3.left;
    }

    if(movementDirection != Vector3.zero) {
        enemy.velocity = movementDirection * stats.speed;
        transform.right = movementDirection;
    }
}

this is assuming your player faces down the X axis. If you want the player to face down the Y axis, use “transform.up = movementDirection”

1 Like

This solution worked, thanks! All I had to do was to change it to transform.up = movementDirection.

1 Like

Glad it worked for you. I always try to avoid Quaternions and setting rotations whenever possible. I can never seem to get them to do what I want. They can probably sense my fear.

1 Like