Play animation OnTriggerEnter (C#)

I’ve never got my head around this but I play animation through a script.
so it writes to the console but doesn’t play an animation.

using UnityEngine;
using System.Collections;

public class DoorTrig : MonoBehaviour {

    public GameObject door;

    void OnTriggerEnter (Collider other)
    {
        if(other.name == "FPSController")
        {
            door.GetComponent<Animation>().Play("DoorOpen"); //Should play the animation
            Debug.Log("Now the animation clip should play");
        }
    }
}

Isn’t it better to use the animator?
(I don’t know how experienced you are, so I’m just gonna tell it in easy talk)

you put an animator on the door object and open the animator window.
from you folder you drag your animation in and from the sounds of it, it could be triggerd from any state, so you right click any state and click on your animation.

then in the left you can add a parameter(I.E. a boolean called close). now click on the arrow of the transition and in the bodem of the inspector add a condition. if you set it to Close and true, it will only play the animation if the boolean is set to true.

in your code, make a reference to your animator


private Animator anim;
anim = GetComponent();

and when your player is within range you say:


animator.SetBool("BOOLNAME"false/true);

so in my examples case


anim.SetBool("walk", true);

Hope it will work :slight_smile:
Sorry if this isn’t what you wanted