The game is 2D platformer.
I have a couple of prefabs that are meant to be animation objects. They play an animation and then destroy themselves. But when I instantiate such a prefab and then change it’s scale then the animator on that prefab plays 1 frame in the set scale but after that reverts back to the original scale at which it was instantiated. This is a problem because I need to for example create a muzzle flash. And when I want to shoot left then the muzzle flash plays in the wrong direction.
It only happens on the animation object prefabs. Doesn’t matter if I change the scale to be larger or negative. It just always comes back to the original scale. If anyone knows please tell me what’s going on here.
This is the AnimationObject script that is on every object of that type:
using Assets.Scripts.Enums;
using UnityEngine;
namespace Assets.Scripts.Animations
{
/// <summary>
/// Playes certain animation and then destroys itself if needed
/// </summary>
public class AnimationObject : MonoBehaviour
{
public bool Loop = false;
public bool DestroyOnFinish = false;
public bool StartOnAwake = true;
public AnimationObjectEnum Animation;
private Animator animator;
public delegate void AnimationEndedDelegate();
public event AnimationEndedDelegate AnimationEnded;
private void Awake()
{
animator = gameObject.GetComponent<Animator>();
if (StartOnAwake)
PlayAnimation();
}
public void PlayAnimation()
{
animator.Play(Animation.ToString(), -1, 0f);
}
/// <summary>
/// Used by animation event
/// </summary>
public void OnAnimationEnd()
{
AnimationEnded?.Invoke();
if (Loop)
{
PlayAnimation();
}
else
{
if (DestroyOnFinish)
{
if (transform.parent != null)
Destroy(transform.parent.gameObject);
else
Destroy(gameObject);
}
}
}
}
}
This is how the animator states look (on the left) + Animator on the right
Important note: when I disable the Animator after instantiating the AnimationObject then the animation doesn’t play but the scale is fine. It actually can be set in code unlike when I keep the animator on.
None of the animations changes the object’s scale or position.
Thank you for your help. If you need more info just tell me.