- Home /
Cant play animation once on collision enter then another animation on collision exit? Called multiple times?
I am having a difficult problem here, reference my Stack overflow question: http://stackoverflow.com/questions/38001740/unity-c-sharp-cannot-make-object-move-down-from-current-y-position-once-on-co
I have platforms that spawn. On collision enter and exit, I want to play an animation ONCE to achieve this effect: When player lands on platform, platform moves down and STAYS DOWN. When they jump up, it moves up and STAYS UP. I've been at this hours - can't get it to work. Im desperate.
I first tried without animations:
void OnCollisionEnter (Collision col) {
GameObject platform = col.gameObject;
//platform.GetComponent<Animation> ().Play ("down1");
if (!playedAnim1) {
platform.transform.position += new Vector3(platform.transform.position.x, -2, platform.transform.position.z);
playedAnim1 = true;
}
touchingPlatform = true;
Debug.Log ("entering platform");
}
Trying to just move the platform 2 down from its original position. This did not work so I looked at http://forum.unity3d.com/threads/play-animation-on-collide.111775/ and created 2 animations. I had to stick my platform prefab in an empty game object so the platform would move just within the empty, so I don't know if this may be causing the issue but with this script:
void OnCollisionEnter (Collision col) {
GameObject platform = col.gameObject;
//platform.GetComponent<Animation> ().Play ("down1");
if (!playedAnim1) {
platform.GetComponent<Animation> ().Play ("down1");
playedAnim1 = true;
}
touchingPlatform = true;
Debug.Log ("entering platform");
}
void OnCollisionExit (Collision col) {
if (!playedAnim2) {
platform.GetComponent<Animation> ().Play ("up1");
playedAnim2 = true;
}
touchingPlatform = false;
Debug.Log ("exiting platform");
}
Despite using a boolean flag like the question, the platform moves up and down repeatedly and doesn't work. I don't know what to do and can't complete my project without this aspect.
I think I had to attach a renderer and collider to the empty in order for the prefab to trigger the oncollisionenter and I think the problem MAY be due to oncollisionenter/exit being called multiple times.. is there a way around this?
How can I play an animation ONCE and have it remain in the final position from that animation (i.e 2 down) when my player collides with the platform, then up when it leaves? Is it possible?
Your answer
Follow this Question
Related Questions
Trigger transition to another animation. 2 Answers
Multiple Cars not working 1 Answer
C#: Climb ledge while inside collider 1 Answer
Distribute terrain in zones 3 Answers
How to set parent collider equal to child collider visually? 2 Answers