- Home /
Simple 2D animation trigger
Hello,
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)
animatedObject.animation.Play();
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!
I really wish I knew what was going on, it seems like such a simple task and it's been driving me nuts all day.
I think first decide if you are using $$anonymous$$ecanim or Legacy. Try not to mix the two components Animator and Animation.
On the object you wish to Animate, in the script which contains the Animation code (do it here as it's already set up here), write a few functions which trigger the animations you wish to play.
Invoke these functions using GetComponent from the Remote object, ins$$anonymous$$d of trying to fire the animation directly.
Answer by tebandesade · Jun 24, 2014 at 07:16 PM
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.
Answer by SteelArrow21 · Jun 24, 2014 at 07:18 PM
OK, this is what I personally would've done:
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; } } }
Attach this script to the piece of floating ground:
using UnityEngine; using System.Collections; public class GroundFall : MonoBehaviour { [HideInInspector] public bool makeFall = false; void Update () { if(makeFall == true) { Animator animator = GetComponent<Animator>() as Animator; animator.SetTrigger("Fall"); } } }
Give the floating piece of ground an Animation Controller.
Give the floating piece of ground the Animator component and set the Animation Controller as the Animator's Animation Controller.
Drag the fall animation onto the Animation Controller Tab.
Add a parameter called "Fall" and make it a trigger.
Add a transition from AnyState to the fall animation and set the condition to "Fall"
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!
Sorry the code is positioned a little far to the right. I was having trouble getting it to work and this is the only way I could.
Hi,
I know this thread is old but I wanted to contribute as I have a similar situation where I have a GameObject with an animation that's the child of my camera that I am trying to trigger via a box collider2D. Anyways, I attached the first script to my trigger and the second to my GameObject. The GameObject has an Animator Component added to it which has the Animator Controller with the ‘Fall’ trigger parameter.
I then selected my trigger within the Hierarchy and dragged the GameObject to the Floating Ground spot and my character GameObject to the ‘Player’ GameObect spot.
When I hit the trigger, the GameOject I attached the second script to, doesn’t play the animation. I added a debug.log after the if statement in the first script and the debug text is displayed within the console. So why is my “other == player” not true? Any advice on how to get this to work would be greatly appreciated.
Actually I figured it out I changed the following:
if (other == player)
to if (other.CompareTag("Player"))
as my character has a player tag added to it.
However now my animation continues looping when I enter the trigger and I only need it to play once.
Thanks for helping me get this far!
remember to turn loop pose off on the animation clip.