good evening,i have 2 game object 1 player 2 enemy , i have 2 animation and animator ,1 for enemy and 1 for player ,i want if i hit enemy by player ,player animation are work but enemy animation does not work ,because i use player component script , and idea to solve that? i try it but does not work
Player script:
public bool killed; //IMPORTANT: The variable must be public so that it can be passed to the enemy script
void OnTriggerEnter(Collider other) //Use "void OnTriggerEnter2D(Collider2D other) If you are creating a 2d game
{
if (other.gameObject.CompareTag("Enemy"))
killed = true;
}
Enemy script:
Animator anim;
bool killed; void Start() { anim = GetComponent<Animator>(); }
void Update()
{
GameObject.find("PlayerGameObject").GetComponent<PlayerScript>.killed; //Replace with the name of the gameobject where the player script is located
if(killed==true)
anim.SetBoolean("name_animation",true);
}
I’m pretty new at this but I have many scripts that access variables from other scripts and I am always told ( by Unity in the error section) that it must not only be public but static. Like, the player script would read :
public static bool killed;
and the enemy script
///////
Monobehavior killed;
public GameObject player; //drag the player into this variable slot
void Start() {
anim = GetComponent<Animator>(); }
void Update()
{
killed = player.GetComponent<PlayerScript>();
if(killed==true)
anim.SetBoolean("name_animation",true);
}
and not sure what your ending does, I know it sets a boolean but where and does that boolean trigger any animation ?
if(killed==true)
anim.SetTrigger("triggerName")//actually I see now, the Animator component has booleans, Never used bools I always use SetTrigger to trigger my animations
okay , but is so different , the code is
private Animator anim;
anim=GetComponent(); in start function
if(Input.GetKeyDown(KeyCode.J)){anim.SetBool(true);}
this code is on game object empty child of player
and the animation is to move coins with animation in unity by start record
he when i play the game animation is done by it self without any button , the game object empty have 2d box collder , and the script without any animation or animator that all thing