- Home /
Multiple colliders on one game object
I want create a chest that will:
a) change color while mouse-hovering
b) check if player is in sphere radius and if is, open after pressing a key.
I created normal, physical box collider that also works while hovering a mouse over it using OnMouseEnter() and OnMouseExit() functions where I change the color back and forth. Point a) works fine.
The problem is, now I want to create another sphere collider to detect if the player is in radius to be able to open the chest. I know Unity doesn't like when GameObject has two colliders because does not recognize them. I read about couple workarounds; making colliders of different type (box, sphere) and checking the type of collision or creating empty child that store my second collider and calling it in parent function. I tried to get an idea how to implement it and ended up with nothing. I'm worried that conditions under which I want to create it (I want collider A to works only on mouse hovering but collider B works only on player collider) limits my ways of doing it. Any solutions?
Answer by DenisIsDenis · Jun 01, 2021 at 01:26 PM
You can use not a spherical collider, but the distance from the player to the chest, the effect will be like a spherical collider. Part of the code:
if(Vector3.Distance(playerObject.transform.position, chest.transform.position) <= sphereRadius)
{
// open chest
}
Works perfectly fine, thanks! Tbh it's kinda useful to think about colliders as a radius distance. Never thought about it but for sure I will try to use it more. Thanks!
Your answer
Follow this Question
Related Questions
Enable tree colliders near you, far ones are disabled 0 Answers
physics.OverlapSphere colliders 1 Answer
Having more than one collider in a GameObject? 8 Answers
Mouse click ON cube to attach another cube to it 1 Answer
Raycast Without Colliders 6 Answers