Simple 2D animation trigger


I wished to have a script that I could place on 2d box colliders that I have set to triggers. I had the idea that I could attach this script and have it play the animation of whichever public transform I have selected. So I wrote the following script:

using UnityEngine;
using System.Collections;

public class AnimationTrigger : MonoBehaviour {

	public Transform animatedObject;

	private CharacterController2D _controller;

	public void Awake(){

		_controller = GetComponent<CharacterController2D> ();


	public void OnTriggerEnter2D(Collider2D col){

		Debug.Log ("Player has triggered the animation!");

		if (_controller != null)

		gameObject.SetActive (false);


It allows me to place the transform in the right place, and the collider recognizes that the player has entered it according to the console. But the animation does not play and I feel that I missed perhaps a pretty small thing in my code.


This is what I am working with. The piece of ground has a ‘fall’ animation, but I removed the animator component from it. Underneath of course is the box collider with the script on it. And the player to the left. Any assistance with what I may have missed would be greatly appreciated. Thanks!

Well you can always try to put some conditions and parameters in the animator such as float, bool,or trigger. In this case I recommend the trigger parameter and name it “x”. Afterwards, you will have to write in the code animatedObject.animation.settrigger(“x”); . This means that when something happens( The character enters, the trigger “x” will be on) Yet, you need to set the animation in the animator with a condition to say that when trigger, the animation plays. Therefore, when “x” is trigger, the animation will play.

OK, this is what I personally would’ve done:

  1. Attach this script to the trigger (You’ll get errors at first, but they’ll disappear soon):

             using UnityEngine;
        using System.Collections;
        public class AnimationTrigger : MonoBehaviour {
            //In the inspector, make this the floating piece of ground
            public GameObject floatingGround;
            //Make this the player
            public GameObject player;
            void OnTriggerEnter2D (Collider2D other) {
                if (other == player) {
                    floatingGround.GetComponent<GroundFall>().makeFall = true;
  2. Attach this script to the piece of floating ground:

             using UnityEngine;
        using System.Collections;
        public class GroundFall : MonoBehaviour {
            public bool makeFall = false;
            void Update () {
                if(makeFall == true) {
                    Animator animator = GetComponent<Animator>() as                Animator;
  3. Give the floating piece of ground an Animation Controller.

  4. Give the floating piece of ground the Animator component and set the Animation Controller as the Animator’s Animation Controller.

  5. Drag the fall animation onto the Animation Controller Tab.

  6. Add a parameter called “Fall” and make it a trigger.

  7. Add a transition from AnyState to the fall animation and set the condition to “Fall”

  8. Please, I insist that you let me know if anything about this is wrong because I will be more than happy to help fix it because I’m kind of just going off what’s in my mind right now.

Also, these scripts are universal meaning they’ll work for any trigger and object just as long as you follow the steps. Hope this helps!