Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question How do I play an animation as SOON as a certain scene is loaded?

Discussion in 'Animation' started by AtinChing, Jul 29, 2020.

  1. AtinChing

    AtinChing

    Joined:
    Apr 6, 2020
    Posts:
    11
    Basically, I have 2D platformer and when the user presses the "Play" button, a scene (which is the "Opening Cutscene") is loaded. The player should start on the ground with his face down and gradually get up, which is the animation I want to play, but when I load the scene, it plays the idle animation once, THEN it plays the getting up from the ground scene. First, I tried to set a trigger in the start method, but it played the idle anim first, then I this
    Code (CSharp):
    1. public class ocLook : MonoBehaviour
    2. {
    3.  
    4.     public Animator anim;
    5.     public GameObject player;
    6.     public GameObject ship;
    7.     public GameObject cam;
    8.     public Camera cc; // Camera component of the camera game object.
    9.     // Start is called before the first frame update
    10.     private void OnEnable()
    11.     {
    12.         SceneManager.sceneLoaded += OnSceneLoaded;
    13.     }
    14.     void OnSceneLoaded(Scene OpeningCutscene, LoadSceneMode Single ) {
    15.         anim = GetComponent<Animator>();
    16.         anim.SetTrigger("GetUp");
    17.     }
    18.     void Start()
    19.     {
    20.         cam = GameObject.Find("Main Camera");
    21.         cc = cam.GetComponent<Camera>(); // Getting camera component from camera.
    22.        
    23.         ship = GameObject.Find("ship");
    24.         ship.SetActive(false); // Set the ship as inactive.
    25.         player = GameObject.Find("Player");
    26.         player.GetComponent<PlayerController>().enabled = false; // Disable the ability to move around
    27.        
    28.         cc.orthographicSize = 15; // The camera size is initially 15 (close to player).
    29.     }
    30.  
    31.     // Update is called once per frame
    32.     void Update()
    33.     {
    34.         if (anim.GetCurrentAnimatorStateInfo(0).IsName("GetUpFromGround")) {
    35.             anim.ResetTrigger("GetUp");
    36.         }
    37.         if (anim.GetCurrentAnimatorStateInfo(0).IsName("lookaround")) {
    38.             cc.orthographicSize = 80;
    39.             ship.SetActive(true); // Activates the ship.
    40.         }
    41.     }
    42. }
    Note: the "ship" is a ship that starts falling from the sky once it is active, it is activated after the player gets p and does the "look around" animation and then it starts falling down to eventually crash in the ground. The ship thing works, but the animation runs Idle first.