- Home /
Why the transfrom.eulerAngles keep reset to 0?
I had a boss script which had a function that rotated the boss to the player
public void LookAtPlayer()
{
if (player.transform.position.x < transform.position.x)
{
transform.eulerAngles = new Vector3(0, 180, 0);
}
else if (player.transform.position.x > transform.position.x)
{
transform.eulerAngles = Vector3.zero;
}
}
I call this public function from another script B (a StateMachineBehaviour script) and placed a break point there. When i ran it line by line it would: Set the eulerAngles.y to 180 -> move to the next line in script B (which is the end curly bracket) -> move out of the OnStateUpdate() and the moment it move out the transform reset to 0. Why? Here script B:
override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
// Before this there is some other code but they did not effect anything
boss.LookAtPlayer();
}
Here is some picture:
Move to the end of script B, the y value still 180:
Go out OnStateUpdate() function and move to another script:
Answer by Long2904 · Jun 08, 2020 at 12:14 PM
I finally knew what was wrong. I had a animation that changed the rotation of the gameObject so after that i couldn't changed it. The easiest fix i got right now is just put the sprite in a child object and rotate that instead.
Answer by UnderShad · Jun 07, 2020 at 01:01 AM
Because of this
if (player.transform.position.x > transform.position.x) // scriptgameobject x is greater than player x { transform.eulerAngles = Vector3.zero; }
@UnderShad that part of the code would never run because it wasn't true and also when i run line by line code it wouldn't run that line. I even tried to set the transform.eulerAngles.y to 180 in the update function and for some reason it just keep resetting to 0.
Your answer
Follow this Question
Related Questions
Setting Rotations 2 Answers
Transform.eulerAngles. x issues 3 Answers
How to use a negative value when MathF.Clamping a eulerAngle? 1 Answer
transform.eulerAngles of camera - changing the speed of rotation not working? 1 Answer
How to get find euler angle provided I have Transformation Matrix 1 Answer