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.
-
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?
-
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.
Figured I would just post the code that I have so far.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Health : MonoBehaviour
{
public int playerHealth = 3;
public GameObject katashiro1;
public GameObject katashiro2;
public GameObject katashiro3;
private Animator animator;
void Start()
{
animator = GetComponent<Animator>();
}
void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Enemy"))
{
Debug.Log("Player was hit by ghost!");
playerHealth -= 1;
Debug.Log("Player Health = " + playerHealth);
katashiro.animator.SetBool("PlayerHit", true);
}
}
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.
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:
void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Enemy"))
{
Debug.Log("Player was hit by ghost!");
playerHealth -= 1;
Debug.Log("Player Health = " + playerHealth);
if (playerHealth == 2)
{
GameObject.Find("Katashiro 1").GetComponent<Animator>().Play("Katashiro_Destroy");
}
else if (playerHealth == 1)
{
GameObject.Find("Katashiro 2").GetComponent<Animator>().Play("Katashiro_Destroy");
}
else if (playerHealth == 0)
{
GameObject.Find("Katashiro 3").GetComponent<Animator>().Play("Katashiro_Destroy");
}
}
}
3 Likes