How to specify in the GetComponent method in colliders?!
I broke my mind in 3 hours of thinking about it... I have UNIT he have several COLLIDERS (2) sphere (for get AGGRESSIVE) and Capsule Collider (not to be cross-cutting)
So I have a ROCKET that detonated when both Rigidbodies OnCollisionEnter() when rocket collision enter to unit collision make Explosion object with PArticle System and script with START method for check colliders from region (like in Unity3d lessons)
BUT problem: my unit have several colliders and one Script which can damage him so when I use foreach to find this creep I get SEVERAL unit-states component coz I have several colliders which Physycs.OverlapsSPhere returned!
//Position Of Explosion
Vector3 explode_pos = this.transform.position;
//for saving creep component
ArrayList creeps = new ArrayList ();
//get colliders from exposion radius
Collider[] colliders = Physics.OverlapSphere(explode_pos, ExplodeRadius);
int i = 0; //test
//search for States component of creeps
foreach(Collider col in colliders) {
//Which If statement using for get ONE StatesComponent from SEVERAL Colliders?
if (col.GetComponent<CommonStatesBars> () == true && col.GetComponent<CapsuleCollider>() == true ) {
i++;
Debug.Log ("Collider#" + i + ", is: " + col.ToString());
}
}
Problem with that i try using DAMAGE to creep states component- i get it SEVERAL times (from both colliders)
Answer by intfloatbool · Mar 23, 2018 at 06:13 AM
Just do not use too many conditions :-)
My solution! :
foreach (Collider col in colliders) {
CommonStatesBars unit = col.GetComponent<CommonStatesBars> ();
if (unit != null) {
unit.MakeDamage (StaticWeapon.MissleDamage);
}
unit = null;
}
Answer by Papnix · Mar 23, 2018 at 08:45 AM
Hello,
As you explain, I understand that you have both colliders set to "collider" (I mean by that not "trigger"). So I suggest you to make your "Agressive" collider a trigger (in the Collider component check "Is Trigger") and when you make your spherecast dont forget to Physics.raycastsHitTriggers == false.
You'll end up normally to only catch the colliders and not the triggers.
If you do that don't forget to change from OnCollisionEnter to OnTriggerEnter to handle your collisions with this new trigger !
I don't try this (I'm at work ; p) but I'm pretty sure it will work.
Have a nice day