- Home /
OnCollisionEnter Animation
Hello Unity3D.I have a question about OnCollisionEnter.Can i use OnCollisionEnter to force the enemy to play an animation without the enemy having any OnTriggerEnter script attached to it?The reason i ask this question is the fact that i am trying to make one of my characters be able to throw a enemy and i was wondering if i needed both parties to have a script or just one of them needs a script for this.If i only need a OnCollisionEnter script for me to be able to throw an opponent.Is this script correct then?
#pragma strict
var sceneCam : GameObject;
sceneCam = GameObject.Find("MainCam");
function OnCollisionEnter(collision : Collision){
if(collision.gameObject.tag == "Dummy") {
sceneCam.transform.position = Vector3(-20.46, 4.258904, -805.3136);
if(animation.IsPlaying("punch1")){
animation.Play ("Hit1");
}
}
}
You only need the collision script to be on/attached to the GameObject that is perfor$$anonymous$$g the action as a result of the collision. You do however need colliders on both.
If you wont to make enemy play animation without OnTriggerEnter() ......... this code help you .......
On your player script:
var enemy : GameObject
function OnCollisionEnter(collision : Collision){
if(collision.gameObject.tag == "Dummy") {
sceneCam.transform.position = Vector3(-20.46, 4.258904, -805.3136);
if(animation.IsPlaying("punch1")){
enemy.animation.Play ("Hit1");
}
}
}
I thought Collision Was better?..I got to stop watching those youtube videos....
Answer by Ericool · Dec 17, 2014 at 09:53 AM
For a collision or a collider you need something like a box collider and a rigidbody on the other gameObject. Careful, you want to play an animation so I suggest you to use OnTriggerEnter
Now i gotcha.But wait ,OnTriggerEnter doesnt that have to be on the emeny ins$$anonymous$$d of the player?also how can i make it that he plays certain animations depending on what the player hit it with.For example i punch them the opponent covers his/her face.or i kick them the opponent goes twirling?