- Home /
OnCollisionEnter
Hello Unity3D.I have a question about OnCollisionEnter?If i want OnCollisionEnter to work at it's best should i add an empty gameobject to the area that i want it to contact the enemy with and add a rigidbody and capsule ?or should i just add it to the armature of the character and add a rigidbody to one of its limb instead?The reason why i ask this questions is the fact that i have seen onCollisionEnter work best when it comes to projectiles instead with close combat(such as swords or fist).If anyone knows which way is best.Can you please tell me why?
It would help alot if you explained the use case of OnCollisionEnter, are you using this for some kind of in game attack/damage system ?
Im Sorry,I really got to be more specific while asking questions....What i am trying to do is make it that when my character hits another character they character that got hit plays an animation depending on what animation that they got hti with.Ill put an example.
#pragma strict
var enemy : Transform;
function Start() {
enemy = GameObject.FindWithTag("Dummy").transform;
}
function OnCollisionEnter(collision : Collision){
if(collision.gameObject.tag == "Dummy") {
if(!animation.IsPlaying("Boeton_Fighting_Stance"))
if(!animation.IsPlaying("punch2"))
if(!animation.IsPlaying("punch3"))
if(!animation.IsPlaying("punch4"))
if(!animation.IsPlaying("Sidekick_lightning"))
if(!animation.IsPlaying("Falling_Axe_$$anonymous$$ick"))
if(!animation.IsPlaying("Shadow_Spinning_Sidekick"))
if(!animation.IsPlaying("Shadow_Calling"))
if(!animation.IsPlaying("para_kick"))
if(!animation.IsPlaying("Windmill"))
if(!animation.IsPlaying("Shadow_Bandit_$$anonymous$$ick_3"))
if(!animation.IsPlaying("One_Hand_Windmill"))
if(!animation.IsPlaying("Rising_knee_Part_2"))
if(!animation.IsPlaying("Jumping_Snap_$$anonymous$$ick"))
if(!animation.IsPlaying("Triple_$$anonymous$$ick"))
if(!animation.IsPlaying("Jumping_$$anonymous$$nee"))
if(!animation.IsPlaying("Shoot_combo_2"))
if(!animation.IsPlaying("Shoot_combo_2_2"))
if(!animation.IsPlaying("Shoot_combo_2_3"))
if(!animation.IsPlaying("Shoot_combo_2_4"))
if(!animation.IsPlaying("Shoot_combo_2_5"))
if(!animation.IsPlaying("Shoot_combo_2_3_001"))
if(!animation.IsPlaying("$$anonymous$$azuya_pause"))
if(!animation.IsPlaying("$$anonymous$$azuya_pause_2"))
if(!animation.IsPlaying("Bullet_Barrage"))
if(!animation.IsPlaying("Bullet_Barrage_Start"))
if(!animation.IsPlaying("Bullet_Barrage_$$anonymous$$id"))
if(!animation.IsPlaying("Star_Blitz"))
if(!animation.IsPlaying("Fast_shot_3"))
if(!animation.IsPlaying("Shoot1"))
if(!animation.IsPlaying("Shoot2"))
if(!animation.IsPlaying("Shoot3"))
if(!animation.IsPlaying("Shoot4"))
if(!animation.IsPlaying("Shoot5"))
if(!animation.IsPlaying("540_Bullets_"))
if(animation.IsPlaying("punch1")){
enemy.animation.Play ("Hit_Stomach");
}
}
}
in the same way you Get a gameobject from the collision component in your Collision you can actually use getcomponent to recieve any component that is upon the Other GameObject the way i would do this would be so that when i make contact with another gameobject i recieve its script for taking damage and then send a message to that script to apply it in this case it would play your animation for being hit, if you would like an example let me know and ill post it as an answer.
Answer by jimjames · Jan 21, 2015 at 04:43 AM
Try adding a empty parent object and see how it works out. Once you figure out the collision bugs and work them out, then add them to your limbs. If you are swinging a sword and you want the enemy to play an animation on hit, try using OnTriggerEnter instead of OnCollision. Ontrigger can still pass over damage amounts and alot more.
void OnTriggerEnter ( Collider other )
{
if(other.gameObject.tag == "Dummy")
{
other.transform.gameObject.animation.Play ("Hit_Stomach"); //I think this is the right wording
}
}
Is all you need. No declaring variables needed. This will be attached to your "sword/fist" and will make the other gameobject play it's animation.
You can also turn on the collider at the beginning of the animation and turn it off at the end so you wont be dealing damage or/or animations to other objects while not swinging.
Hope this helped and what you are after.
James
Yes it does answer my question but the last thing i need to know before i can fully understand OnCollisionEnter and OnTriggerEnter.How can i make it that when i hit an opponent with a certain animation the person that got hit with that animation does a certain animation?the reason is that i want my characters to grab each other and to do that they have to be able to do certain animations to each other I have a script for an example #pragma strict
var setOnFire : ParticleSystem;
var player :Transform;
var speed = 30;
var pushPower = 2.0;
var anim: String;
var cam: Camera;
function OnTriggerEnter (Col : Collider)
{
if (Col.tag == "hitbox2")
if (Col.tag == "Sword2")
{
// Default hit animation
var anim : String = "Hit1";
// Animation related to player's animation
if(Col.animation.IsPlaying("punch2"))
anim = "Hit_Stomach";
anim = ("Hit_Stomach");
animation.Play("Hit_Stomach");
cam.animation.Play("HitAnimationCamera");
cam.animation["HitAnimationCamera"].speed = 2;
setOnFire.Play();
}
}
function OnTriggerExit (other : Collider)
{
if (other.tag == "Player")
{
cam.animation.Play("Normal Camera");
}
}
a sample of how to get a script component from the object u trigger. this the other gameobject will need a javascript named TakeDamage been a while since i've written javascript so let me know if it doesn't work and ill play with it.
private var takeDamage : TakeDamage;
function OnTriggerExit(other : Collider) {
takeDamage = other.GetComponent("TakeDamage");
}
then in the same manor you would fire a function from within a script u would use the following to make the other script do your custom function
takeDamage.PlayAnim();
so upon the script TakeDamage you would require a function similar to the following.
function PlayAnim() {
animation.Play("Hit_Stomach");
}
Thanks So much i am going to try this right now =D