- Home /
How can I get Compound Colliders to work properly?
I currently have a Sphere object in the library, that I instantiate at the start of the game:
As you can see from the inspector of the Drone parent object, I have a rigidbody on this object. It does not have a collider. The DroneTrigger object only has a collider on it. I've read that with this setup, the child object's collider will take over for the parent if one of the parent scripts calls OnTriggerEnter. I have yet to make this work as intended.
I am trying to hit the DroneTrigger with a bullet object that I'm firing. When I was just using a collider on the Drone object, it worked fine.
I want this to eventually allow me to use a second child collider for a different collision.
EDIT: I'd like to add that when the player object enters the DroneTrigger trigger, it passes the Player collider fine. It just does not seem to like the one from the Bullet. Before you tell me, I've tried using FixedUpdate on the bullet.
What is the purpose of the Drone Trigger script? And the Drone script? Can you tell us how you are trying to use OnTriggerEnter?
Right now the DroneTrigger script has no code. The Drone script has this:
public class Drone : $$anonymous$$onoBehaviour
{
private Enemy$$anonymous$$anager manager;
public int health;
public int damage;
public float speed;
void Awake()
{
manager = GameObject.Find("SpawnPoint").GetComponent<Enemy$$anonymous$$anager>();
}
// Use this for initialization
void Start ()
{
health = 20;
speed = 5f;
damage = 1;
}
// Update is called once per frame
void Update ()
{
if(manager.target != null)
transform.position = Vector3.$$anonymous$$oveTowards(transform.position, manager.target.collider.bounds.center, speed * Time.deltaTime);
}
public void OnTriggerEnter(Collider col)
{
Collision$$anonymous$$anager.BulletTriggerEnter(col, gameObject);
}
}
This function is in Collision$$anonymous$$anager:
public static void BulletTriggerEnter(Collider otherCollider, GameObject drone)
{
if(otherCollider != null && drone != null)
{
if(otherCollider.tag == "Bullet")
{
otherCollider.GetComponent<Bullet>().Deactivate();
if(drone.GetComponent<Drone>().health <= 0)
Destroy(drone);
else
drone.GetComponent<Drone>().health -= Weapon$$anonymous$$anager.Damage;
}
}
}
Answer by Owen-Reynolds · Oct 04, 2013 at 03:01 PM
Triggers don't "understand" compound colliders the way other colliders do (NOTE: Unity v4.01f2.) A fix is to add a "find my parent" to them.
As you note, OnCollisionEnter is fine with a rigidbody parent with no collider, but with any number of child non-RB colliders. Hits on any kid count as hits on the parent, and you can check which exact kid it was. In code, collision.transform
is the main RB object, and collision.collider
is the particular kid.
But, with triggers, they only tell you the particular collider that entered/left/stayed. Even worse, If kid1 enters, and then kid2, they count both -- triggers are too "dumb" to know parentRB + kid1 + kid2 form one compound collider object.
A workaround is to find the parent yourself:
void OnTriggerEnter(Collider cc) {
Transform T = cc.transform;
while(T.parent!=null) T=T.parent;
Or depending on how objects are set up, go up parents until you get the correct tag, find a rigid body ... .
Your method works fine, it's just that I cannot get the trigger to call the OnTriggerEnter function when the Bullet enters the trigger. The Player passes its collider just fine when I run into the Drone object.
Again, the Bullet was hitting the trigger when was using just the parent object with a collider and RB.
I was able to get the bullet to enter the trigger properly. I didn't realize I had collision between the Bullet layer and the Default layer off.
I'm going to add the other colliders/triggers I wanted to add, and see if I have any problems.
Answer by SilentSin · Oct 04, 2013 at 01:36 PM
The OnTriggerEnter will go to either the object with the Rigidbody or the object with the collider. I'm not sure which, but evidently its the one you aren't using.
Your answer
Follow this Question
Related Questions
Compound collider does not trigger the parent 1 Answer
Odd character controller behavior when a shield object parented to child 0 Answers
collision.gameObject returning parent. 1 Answer
How can I check the colliders of objects instantiated into the Scene 1 Answer
Having trouble understanding parenting with componants 0 Answers