- Home /
How can i detect collision in melee combat?
Hi to all
In my game i have two group of ai characters(i.e. red and blue)and red ai could attack to blue's.my npc's have a "charactercontroler" component and their attack animation is melee type such as punching or Spurn attack without any weapon.Since my npc's collider is a capsule like(charactercontroler)and their hand or leg dose not had collider,how can i detect colliding with other npc's?
I try adding a separate collider(capsule collider) to their hand and leg,but it did'nt work.
any suggestion or help would be appreciated. excuse me for my bad english.
Similar questions that could help: http://answers.unity3d.com/questions/37093/recommended-approach-for-melee-combat-system.html
http://answers.unity3d.com/questions/193996/melee-combat.html
http://answers.unity3d.com/questions/160747/ontriggerenter-melee-combat.html
http://answers.unity3d.com/questions/63009/what-is-the-best-way-to-do-third-person-melee-comb.html
Excuse me for late reply. i get look at first link and it was helpful. I add some empty gameobject to npc's hand and foot and added capsule Collider's to them and used oncollisionEnter for detecting collisions and modify npc health,but a weird thing happened.It seems body part Colliers (hand and foot) collide eachother and it cause in function ,same object destroy itself Is it possible?
@bigbat it is possible. To avoid this, in your OnCollisionEnter function, add a check for a specific tag so that you can check the collider object and only perform the function on the right objects.
void OnCollisionEnter(Collision collision)
{
if(collision.transform.tag == "WhatImLookingFor")
{
// bla bla bla
}
}
@$$anonymous$$avio thanks for advice. Now i have another issue,as explained in unity documentation for colliding i add rigidbody to my npc's,but my npc's start moving in world with very high speed and unexpected movement(for clarification:my npc's use unity Nav$$anonymous$$esh and Nav$$anonymous$$eshAgent for moving in game world,and i dont use any physic related movement such as addforce ,and before i add rigidbody to them they act correctly and all things was ok). if i check "is$$anonymous$$inematic" field of rigidbody, the problem of movement resolve but else again no collision occure between npc's. whats wrong in my setup? thanks for your time and help.
Answer by bigbat · Jul 05, 2013 at 06:45 PM
For future readers: I add some empty gameobject to npc's hand and foot and added capsule Collider's to them and use oncollisionEnter for detecting collisions and modify npc health. For solving weird npc movement i enable all X, Y and Z parameters of Freeze Position and Freeze Rotation in Constraints of rigidbodies(i leave "IsKinematic" and "use gravity" fields unchecked). here is my collision detection script:
void OnCollisionEnter(Collision collision)
{
int dmg;
if(collision.gameObject.layer!=gameObject.layer&&collision.gameObject.layer!=0)
{
if(GetComponent<SimpleAI>().currentstate==SimpleAI.aistate.ais_attack)
{
dmg=GetComponent<AIHealth>().DamageRank;
if(collision.gameObject.tag!="Player")
collision.gameObject.GetComponent<AIHealth>().SetAIHealth(- dmg,0) ;
else
collision.gameObject.GetComponent<playerstatus>().SetPlayerStatus(-dmg,0);
Debug.Log(collision.gameObject.name+ " is damaged by "+name+" by damage "+dmg+"time:"+Time.time);
}else
{
Debug.Log(gameObject.name+"collided with: "+collision.gameObject.name+" and not attacking");
}
}
}
Your answer
Follow this Question
Related Questions
Melee attack comblat 1 Answer
Melee Combat 1 Answer
Multiple Melee Weapons 2 Answers
Colliding Melee Weapons 2 Answers