1
\$\begingroup\$

I have a script to control the player simply moves Left, move right, etc.. but the animation when winning, losing, or Light, just put it on the animation and animator and no script for it.

I wanted to ask. if player enter at the collider, how to call animation light ?

void OnTriggerEnter2D(Collider2D theCollider)
    {
        try
        {
            // how can i call animation light //
        }
        catch { /*On error do nothing*/ }
    }
\$\endgroup\$
1
\$\begingroup\$

If you are using Mecanim (which, by your question, I guess you're doing), you can create for example a trigger variable in the Animator, and set it from your code wherever you want. Triggers are basically like booleans, but you don't specify any true/false value. Instead, you just set them from your code, and they will be "true" just for a moment. After the transition has been made, Unity deactivates the trigger automatically.

Animator animator = GetComponent<Animator> ();

void OnTriggerEnter2D (Collider2D col) {
    animator.SetTrigger("light");
}

This will cause the trigger variable "light" that you have set up in your animator (using the animator window where you have all the transitions between states and so on) to be active just for a moment, before deactivating it automatically after the transition to the Light animation has been made.

|improve this answer|||||
\$\endgroup\$
1
\$\begingroup\$

If you are using the legacy animation system you can simple call Play from the Animation component.

For mecanim I think that you need to change some variable into animation state machine that triggers the animation, check out:

|improve this answer|||||
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.