- Home /
Why my animation change my object rotation
I'm new to Unity. I'm trying to create a planet model with some random plants in it. I 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");
}
}
}
So when i play it, it shows :
Every time the player collides with the plant, the plant will rotate a bit, but when my player rotates a few times and triggers the plant animation, it looks like this:
The problem might be that the animation itself sets the rotation of an object, and if the object has no parent it changes rotation relative to global coordinates. The solution woluld be to use empty gameobject as a rotation pivot for your trees.
Your answer
![](https://koobas.hobune.stream/wayback/20220613041851im_/https://answers.unity.com/themes/thub/images/avi.jpg)