unity-game-engineanimationanimator

Why animation with rotation can break my in-game object rotations?


I'm new to Unity. I'm trying to create a planet model with some random plants in it. I use empty game object as a parent to spawn plant with random sprite when the game start. I also added a scipt to play some animations when the player collides with the plants. But I noticed that every time the animation plays my plant angle decreases by a certain degree. I use this code to make the plant spawner:

public class Tree : MonoBehaviour
 {
     
     public Transform customPivot;
     public float angle;
     public GameObject plant;
     
     void Start()
     {
         
        transform.RotateAround(customPivot.position,Vector3.forward,angle);
        Vector3 pos=this.transform.position;
        GameObject myPlant= Instantiate(plant,pos,Quaternion.identity) as GameObject;
        myPlant.transform.SetParent(this.transform);
        myPlant.transform.Translate(0,0,0);
        myPlant.transform.localEulerAngles = new Vector3(0,0,-90);
     }
 }

And i play the plant animation using this code:

 public class TreeBehaviour : MonoBehaviour
 {
    
     private int rand;
     Animator animator;
     public Sprite[] Sprite_pic;
     void Change(){
         rand=Random.Range(0,Sprite_pic.Length);
         GetComponent<SpriteRenderer>().sprite=Sprite_pic[rand];
     }
 
     private void Start() {
         Change();
         animator=GetComponent<Animator>();
         
     }
     private void OnTriggerEnter2D(Collider2D other) {
         if(other.CompareTag("Player")){
             animator.SetTrigger("Trigger");
         }
     }
 }

If player collide with the plant, it will trigger plant animation. The animation just simply rotate the plant to certain degree, and then rotate it back to zero degree. When i play it, it shows : enter image description here

Every time the player collides with the plant, the plant animation will rotate the plant a bit and the rotate it back, but when my player rotates a few times and triggers the plant animation, it looks like this: enter image description here

It seems like every time the animation plays, after the animation stops, my plant rotation changes slightly. I'm confused because I've set the animation to roll back to zero degrees. Does anyone know what I'm doing wrong?

Edit: So as suggested, i try to untick the root motion but i get this: enter image description here

It looks like all the angles have gone to zero degrees. I tried changing the angle in the Transform section when the game is playing but the angle is still zero degrees. I try to add:

void Update()
    {
        this.transform.localEulerAngles = new Vector3(0,0,-90);
    }

But the angle still at 0 degree


Solution

  • If I got you right, it might be a problem with Root Motion that applies the changes to the transform (learn more about it here)

    Unticking Apply Root Motion in Animator Component

    Try un-ticking this and tell me if it fixed it.