- Home /
Can I choose the right collider?
Hello!
I have a game object with 2 colliders (sphere, cube).
How can I run some code when the cube collider only collide with something else?
If I put
function OnCollisionEnter ()
{
}
it runs the code when any of the 2 colliders collide...
Answer by Fattie · Jun 09, 2013 at 10:34 AM
You can only have one collider on one game object.
But what about this. Make an empty game object called say "spaceship"
Now make an empty game object with a sphere collider. And also make an empty game object with a cube collider.
In your hierarchy drag those two under the "spaceship". This means the spaceship is the "parent" of the other two.
Now put this script only on the cube collider object
function OnCollisionEnter ()
{
Debug.Log("this is the cube collider speaking, and I got hitted");
}
And put this different script only on the sphere collider object
function OnCollisionEnter ()
{
Debug.Log("I'm Mr. Sphere I got bashed");
}
Next. On the actual spaceship object, have this script, called TheBoss.js
function Awake()
{
Debug.Log("the boss is running.");
}
function newsFromBelow()
{
Debug.Log("something happened underneath me!");
}
So far so good! Finally in the script on the cube collider,
on a collision, do this
transform.parent.GetComponent(TheBoss).newsFromBelow();
Maybe that will help.
glad to help, looking forward to your next question. pls go through my question and add a few thumbs-up so I get moar points ;-)