- Home /
Question by
Crax · Dec 16, 2013 at 03:34 PM ·
animationjavascriptcollision
Animation won't start when collision starts
I am reading "Unity 3.x game development essentials" and i am blocked at "adding the coconut shy shack" paragraph. I wrote all the code for make the target play an animation when it collides with a coconut, the code is good, but when i instantiate a coconut and it hits the target, the target won't start the animation. This is my code:
#pragma strict
private var beenHit:boolean=false;
private var targetRoot:Animation;
var hitSound:AudioClip;
var resetSound:AudioClip;
var resetTime:float = 3.0f;
function Start () {
targetRoot=transform.parent.transform.parent.animation;
}
function Update () {
}
function OnCollisionEnter(theObject:Collision){
if (beenHit==false && theObject.gameObject.name=="Coconut"){
StartCoroutine("targetHit");
}
}
function targetHit(){
audio.PlayOneShot(hitSound);
targetRoot.Play("down");
beenHit=true;
yield new WaitForSeconds(resetTime);
audio.PlayOneShot(resetSound);
targetRoot.Play("up");
beenHit=false;
}
this is my scene setting: Image1
And this is my target's animations setting:
Comment
Your answer
Follow this Question
Related Questions
Can someone help me with my script? 0 Answers
Attack animation Wont Play 1 Answer
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Setting Scroll View Width GUILayout 1 Answer