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 *SOLVED* - Best way to trigger an animation on a different game object?

Discussion in 'Animation' started by Jacksonion, Aug 23, 2022.

  1. Jacksonion

    Jacksonion

    Joined:
    Nov 19, 2021
    Posts:
    7
    Disclaimer, I'm very new to all of this, so thank you in advance for any help!

    The game that I'm making has 3 game objects in the bottom-right corner of the screen to indicate the remaining "lives" that the player has. Upon the player getting hit by an enemy, I want to trigger the animation that I've made for one of those game objects. The animation that I've made essentially destroys the life object to show the player that they've lost a life.

    I've made a script already on the player game object that decrements their lives by 1 when they get hit. And I've made the whole sequence in the animator, as well as a "PlayerHit" bool parameter. Now I just need to know 2 things.

    1. How can I tell the script to only start the animation on the first life symbol when getting hit the first time, and then starting the animation on the second life symbol when getting hit the second time... etc?

    2. How can I reference the life symbol game objects from the player health script that I've made? (This is the script that I made the collision detection code on). Again, it's attached to the player right now.

    If needed, I will provide the code that I already have.

    Sorry if this is confusing. It's difficult to phrase questions when I don't know what I don't know, I'm just pretty lost at the moment. Please let me know if I need to clarify anything.
     
  2. Jacksonion

    Jacksonion

    Joined:
    Nov 19, 2021
    Posts:
    7
    Figured I would just post the code that I have so far.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Health : MonoBehaviour
    6. {
    7.     public int playerHealth = 3;
    8.     public GameObject katashiro1;
    9.     public GameObject katashiro2;
    10.     public GameObject katashiro3;
    11.  
    12.     private Animator animator;
    13.  
    14.     void Start()
    15.     {
    16.         animator = GetComponent<Animator>();
    17.     }
    18.  
    19.  
    20.     void OnTriggerEnter(Collider other)
    21.     {
    22.         if (other.CompareTag("Enemy"))
    23.         {
    24.             Debug.Log("Player was hit by ghost!");
    25.             playerHealth -= 1;
    26.             Debug.Log("Player Health = " + playerHealth);
    27.             katashiro.animator.SetBool("PlayerHit", true);
    28.         }
    29.     }
    katashiro is what the game objects for the remaining lives are called. I basically just made empty gameobjects and dragged each katashiro game object into its appropriate box in the inspector. not sure if that's the right way to do that.
     
  3. Jacksonion

    Jacksonion

    Joined:
    Nov 19, 2021
    Posts:
    7
    Looks like the syntax to do this is:

    GameObject.Find("Name Of My Object That has an animation").GetComponent<Animator>().Play("AnimationName");


    so in my case, it was:

    Code (CSharp):
    1.  void OnTriggerEnter(Collider other)
    2.     {
    3.         if (other.CompareTag("Enemy"))
    4.         {
    5.             Debug.Log("Player was hit by ghost!");
    6.             playerHealth -= 1;
    7.             Debug.Log("Player Health = " + playerHealth);
    8.             if (playerHealth == 2)
    9.             {
    10.                 GameObject.Find("Katashiro 1").GetComponent<Animator>().Play("Katashiro_Destroy");
    11.             }
    12.             else if (playerHealth == 1)
    13.             {
    14.                 GameObject.Find("Katashiro 2").GetComponent<Animator>().Play("Katashiro_Destroy");
    15.             }
    16.             else if (playerHealth == 0)
    17.             {
    18.                 GameObject.Find("Katashiro 3").GetComponent<Animator>().Play("Katashiro_Destroy");
    19.             }
    20.         }
    21.     }
     
    elijahmtt and VayRose like this.