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.