Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Objects reverting to previous scale

Discussion in 'Animation' started by unity_Duhyw-5LdCKlhQ, Aug 17, 2023.

  1. unity_Duhyw-5LdCKlhQ

    unity_Duhyw-5LdCKlhQ

    Joined:
    Dec 1, 2020
    Posts:
    2
    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:


    Code (CSharp):
    1. using Assets.Scripts.Enums;
    2. using UnityEngine;
    3.  
    4. namespace Assets.Scripts.Animations
    5. {
    6.     /// <summary>
    7.     /// Playes certain animation and then destroys itself if needed
    8.     /// </summary>
    9.     public class AnimationObject : MonoBehaviour
    10.     {
    11.         public bool Loop = false;
    12.         public bool DestroyOnFinish = false;
    13.         public bool StartOnAwake = true;
    14.         public AnimationObjectEnum Animation;
    15.         private Animator animator;
    16.  
    17.         public delegate void AnimationEndedDelegate();
    18.         public event AnimationEndedDelegate AnimationEnded;
    19.  
    20.         private void Awake()
    21.         {
    22.             animator = gameObject.GetComponent<Animator>();
    23.  
    24.             if (StartOnAwake)
    25.                 PlayAnimation();
    26.         }
    27.  
    28.         public void PlayAnimation()
    29.         {
    30.             animator.Play(Animation.ToString(), -1, 0f);
    31.         }
    32.  
    33.         /// <summary>
    34.         /// Used by animation event
    35.         /// </summary>
    36.         public void OnAnimationEnd()
    37.         {
    38.             AnimationEnded?.Invoke();
    39.  
    40.             if (Loop)
    41.             {
    42.                 PlayAnimation();
    43.             }
    44.             else
    45.             {
    46.                 if (DestroyOnFinish)
    47.                 {
    48.                     if (transform.parent != null)
    49.                         Destroy(transform.parent.gameObject);
    50.                     else
    51.                         Destroy(gameObject);
    52.                 }
    53.             }
    54.         }
    55.     }
    56. }
    57.  
    This is how the animator states look (on the left) + Animator on the right
    upload_2023-8-17_21-26-37.png

    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.
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,161
    This is on the physics forum so I'm wondering what the physics part of the question is?

    If it's nothing to do with physics then I can move it to the Animation forum for you because it looks like an animation only post.
     
  3. unity_Duhyw-5LdCKlhQ

    unity_Duhyw-5LdCKlhQ

    Joined:
    Dec 1, 2020
    Posts:
    2
    Yes please, thank you. I didn't realize. :)
     
    MelvMay likes this.