R
R
ROWASTAIR2021-04-23 14:28:39
Unity
ROWASTAIR, 2021-04-23 14:28:39

How to play animation from another object?

There is an Enemy on whose arm is a collider capsule. I need an animation for taking damage. When this collider comes into contact with the player's collider, his life is taken away. I also have a script where animations are played. How can I pass the animation play value from other code?
Collider contact code:

private void OnTriggerEnter(Collider collider)
    {
        GameObject dam = GameObject.FindWithTag("Player");
        Health dhealth = dam.GetComponent<Health>();
        dhealth.health -= damage;
    }

I tried creating a variable that accepts an animator component and inserting the animator from the player there. But still nothing happens. Just by doing like this in the script above:
public Animator anim;

    void Start()
    {
       anim = GetComponent<Animator>();
    }

    private void OnTriggerEnter(Collider collider)
    {
        GameObject dam = GameObject.FindWithTag("Player");
        Health dhealth = dam.GetComponent<Health>();
        dhealth.health -= damage;
        anim.SetTrigger("Damage");
    }

6082af349ed98775390275.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
ReyGrau, 2021-04-23
@ReyGrau

Make some GetDamage(int damage) method on the player and do everything there, and call it through your enemy.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question