- Home /
mecanim animation transition script
Lads,
I'm using mecanim and trying to trigger a transition from one animation state to another. I have set up a trigger parameter (should I be using bool?), and would like to trigger the transition between the states when a collider is entered. I have colliders on my character, and the colliding game object. The script runs without errors, it just doesn't work. Here's the code I have so far:
using UnityEngine;
using System.Collections;
public class Strike_Tower : MonoBehaviour
{
Animator anim;
void Start(){
anim = GetComponent<Animator>();
}
void OnTriggerEnter(Collider other)
{
if(other.transform.tag == "Tower")
{
anim.SetTrigger("Trigger");
}
}
}
Any thoughts?
Thanks,
Stef
Answer by joshh · Jul 17, 2014 at 07:57 PM
Are you using a 2D collider? Is collider on the same object as the animator?
I am using a capsule collider. The collider is on the same object as the animator.
If you put a Debug.Log() in the if(other.transform.tag == "Tower") block does it print? $$anonymous$$y guess without the scene is that OnTriggerEnter isnt happening or the transform tag is not registering as "Tower"
Answer by AndyMartin458 · Jul 17, 2014 at 08:13 PM
You should be using code to set a parameter, not a trigger in the anim. For Instance, if your mecanim system has a bool called triggering, then you would use anim.SetBool("triggering", true);
. Then you would have a transition in your mecanim system when that bool is set to true.
edit__ after reading more about Triggers, they seem to work like booleans except that are set to inactive after they happen. In that case, have you set up a transition to happen when the parameter becomes true?
Thanks Andy, Yes, I have tried setting up the SetBool trigger as you suggest, but still don't get the result. Also, yes, I have tried setting up the Trigger as you suggest, but still no result... :S. Here's what my code currently looks like: using UnityEngine; using System.Collections;
public class Strike_Tower : $$anonymous$$onoBehaviour { Animator anim;
void Start(){
anim = GetComponent<Animator>();
}
void OnTriggerEnter(Collider col)
{
if(col.gameObject.tag == "Tower")
{
anim.SetBool("Hit", true);
}
}
}
Answer by Stef · Jul 17, 2014 at 10:41 PM
I was just missing a rigid body collider on my character... duh. Thanks all.
Your answer
Follow this Question
Related Questions
Creating a collider in a gameObject using only code 1 Answer
How to get default animation for layer 0 Answers
Call a Void on Collision 2 Answers
Distribute terrain in zones 3 Answers
Mecanim animator layers 0 Answers