- Home /
How play an animation through collision (2D Game)?
Hello everyone! I'm new here!
I need help whit a animation. I wanna the girl from my game eat a food. I'm trying to make a collision between the food and the girl. When collide, the girl will start the animation "eat". My animation is made whit .png images (it's a 2D Game).
I make all the transitions:
The parameter "eat" is trigger and the transition from "Any State" to "eat" is it:
The GameObject "girl":
And the GameObject "food"(the script is here with the name "foodScript"):
And it's the code from "foodScript" (Java, but no problem with C#):
#pragma strict
var girl : Animation;
function OnTriggerEnter2D(Other : Collider2D)
{
if(Other.gameObject.name == "girl")
{
girl.Play("eat");
}
}
I don't know why my animation doens't start... I 'm in it for two days. I looked for answers but found nothing... So... It's it guys! If anyone can help me, please, do it. I don't know where i'm going wrong. It's Unity 5.0.0f4.
If you're going to use OnTriggerEnter then make sure that one of objects collider's IsTrigger is set to true and in the image you've shown you haven't assigned the animation to the girl variable in the inspector.
Answer by jakhir1 · Apr 01, 2015 at 02:33 PM
You're using the animator, The code you're using won't work like that. Try this instead.
var girl : Animator;
function OnTriggerEnter2D(Other : Collider2D)
{
if(Other.gameObject.name == "girl")
{
girl.SetTrigger("NameOfTrigger");
}
}
Answer by Notter · Apr 01, 2015 at 02:24 PM
You need to use the Animator for the girl, and then activate it's trigger. not just tell the animation "play".
like this:
public Animator GirlAnimator;
GirlAnimator.SetTrigger("eat");