- Home /
How to use multiple Colliders for an enemy to detect multiple collisions in different areas to determine the damage to the enemy? I.E. Headshot etc.
Hello,
http://imageshack.us/photo/my-images/849/problemd.png/
Heres the code that im using.
function ApplyDamage (damage : float) { // We already have less than 0 hitpoints, maybe we got killed already? if (hitPoints <= 0.0) return;
hitPoints -= damage;
if (hitPoints <= 0.0) { Detonate(); }
}
function Detonate () { // Destroy ourselves Destroy(gameObject);
if(sHH == 1 || mHH == 1){
warRpgScript.playerExp += warRpgScript.enemyExp;
mageRpgScript.playerExp += mageRpgScript.enemyExp;
}
// Play a dying audio clip if (bodydieSound) AudioSource.PlayClipAtPoint(bodydieSound, transform.position);
if (deadReplacement) { var dead : Transform = Instantiate(deadReplacement, transform.position, transform.rotation);
// Copy position & rotation from the old hierarchy into the dead replacement
CopyTransformsRecurse(transform, dead);
}
}
function Detonate2(){ // Destroy ourselves Destroy(gameObject);
if(sHHH == 1 || mHHH == 1){
warRpgScript.playerExp += warRpgScript.enemyExp;
mageRpgScript.playerExp += mageRpgScript.enemyExp;
}
// Play a dying audio clip if (headdieSound) AudioSource.PlayClipAtPoint(headdieSound, transform.position);
// Replace ourselves with the dead body if (deadReplacement2) { var dead : Transform = Instantiate(deadReplacement2, transform.position, transform.rotation);
// Copy position & rotation from the old hierarchy into the dead replacement
CopyTransformsRecurse(transform, dead);
}
}
static function CopyTransformsRecurse (src : Transform, dst : Transform) { dst.position = src.position; dst.rotation = src.rotation;
for (var child : Transform in dst) {
// Match the transform with the same name
var curSrc = src.Find(child.name);
if (curSrc)
CopyTransformsRecurse(curSrc, child);
}
}
function OnControllerColliderHit(hit : ControllerColliderHit){
if(hit.gameObject.tag == "sword"){
sHH = 1;
ApplyDamage(Random.Range(12,24));
bodyswordHHit = 1;
var tempbodyswordHit: Transform;
tempbodyswordHit = Instantiate(bodyswordHit, transform.position, transform.rotation);
var tempbodyswordHit2: Transform;
tempbodyswordHit2 = Instantiate(bodyswordHit2, transform.position, transform.rotation);
}
if(hit.gameObject.tag == "magic"){
mHH = 1;
ApplyDamage(Random.Range(2,10));
bodyswordHHit = 1;
var tempbodymageHit: Transform;
tempbodymageHit = Instantiate(bodymageHit, transform.position, transform.rotation);
}
}
function OnTriggerEnter( otherObject : SphereCollider){
if(otherObject.gameObject.tag == "sword"){
sHHH = 1;
ApplyDamageHead(Random.Range(34,48));
headswordHHit = 1;
var tempheadswordHit: Transform;
tempheadswordHit = Instantiate(headswordHit, transform.position, transform.rotation);
var tempheadswordHit2: Transform;
tempheadswordHit2 = Instantiate(headswordHit2, transform.position, transform.rotation);
}
if(otherObject.gameObject.tag == "magic"){
mHHH = 1;
ApplyDamageHead(Random.Range(2,10));
headswordHHit = 1;
var tempheadmageHit: Transform;
tempheadmageHit = Instantiate(headmageHit, transform.position, transform.rotation);
}
}
function OnCollisionExit(collisionInfo : Collision){
sHH = 0; mHH = 0; sHHH = 0; mHHH = 0;
}
Just to clear things the weapon im using has a capsule collider that has isTrigger checked and it has a kinematic rigid body also (not on the same object).
Even though i have two different functions for collisions im not getting any collisions at all. If i use function OnTriggerEnter( otherObject : SphereCollider) i get no collisions. If i replace that function with function OnTriggerEnter( otherObject : Collider) without the sphere, i get collision from every collision even from the sphere collider. I have been searching for an answer for hours.
If anyone can help much thanks!
Answer by Cyb3rManiak · May 08, 2011 at 07:50 AM
Whoa. Lots of information...
Hope I got it all correctly.
About the collision issue - when you use the OnTriggerEnter( otherObject : Collider) method, and say you get collisions from every collider - that's ok. That's normal behavior. If you want to check the type of the collider, you can take the one you received and attempt to cast it. Since SphereCollider inherits from Collider, if in fact the collider you received was a Sphere collider, these next lines should work:
var colliderAsSphere = otherObject as SphereCollider;
if (colliderAsSphere == null)
{
// This is not a sphere collider...
}
else
{
// This IS a sphere collider...
}
I must admit that I don't recall if I tried it myself, but in theory it should work.
I don't really get why you're trying to find a sphere collider though, since you said your weapon has a capsule collider... If the script is on your character's head, otherCollider will hold the sword, which has a capsule collider.
Anyway, the problem with this approach is that you assume that you will only have sphere (or capsule colliders) on your weapons. Not very agile. I know you check the tag of the hit colliders, so I don't really see why you need to check the type of the collider. If the intention is to filter out unnecessary collisions, why not use Layer-Based Collision Detection?
I would go about your problem differently. I would attach the script that detects collisions to the to the weapon, and leave the health and effects script on the character. You can have a script for the head, and a script for the body (Or better yet - have a script called "BodyPart", and put one on the head, and one on the body). Have those scripts reference the main script of the character they sit on. Make sure they can call methods on the main script. No need for static stuff here. Each BodyPart script should just have a
private var parentCharacterScript: YourCharacterScript;
in it, so it can directly call it's parent. Obviously you can make it public, and drag the main script to it using the inspector, if that's easier for you.
When the weapon hits a collider, it searches for a "BodyPart" script on it. If it finds it, it will trigger a method like
function HitBy(weapon: WeaponScript)
{
...
}
What that method in turn will do is call the main script of the character it sits on and relay the information. The main script will get what part was hit (since that part was the one that activated the method), and will receive what weapon hit that part. You can pass the "weapon" variable from the HitBy method, or you can simply send the name of the weapon as a string...
If you want to make it more straight-forward (and in my opinion less flexible), you can skip the body parts scripts and when the weapon hits a collider - search up the hierarchy of the hit collider until you reach the top-most GameObject or Transform, and search for a "MyCharacter" script (or whatever you call it), and transfer the hit information directly to it.
Hope it helps and I'm not just yammering because I misunderstood you.
Your answer
![](https://koobas.hobune.stream/wayback/20220613132539im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Gameobject not registering collisions 1 Answer
Temporary invulnerability in a platformer 1 Answer
Need help with a trigger problem? 1 Answer
Trigger and ennemy damage 2 Answers
OnTriggerEnter - multiple collisions with same object 0 Answers