- Home /
Collider inside another collider : I have these spheres inside the brain, and I want to be able to move the brain via mouse while being able the click the spheres
The spheres have a sphere collider on them and are clickable via this script:`Clickable.cs` public class Clickable : MonoBehaviour { void Update() { ClickDetect();
}
void ClickDetect()
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
Transform t = hit.collider.transform;
if (t.name == "Sphere(Clone)")
{
if (Input.GetMouseButtonDown(0))
{
hit.collider.gameObject.GetComponent<Light>().color = new Color(1, 0, 0, 0.7f);
Debug.Log(t.parent.name + (t.GetSiblingIndex() + 1));
}
}
}
}}
and the brain has a box collider on it and is moved by this script RotateonMouse
`void Rotation() { if (_isRotating) { // offset _mouseOffset = (Input.mousePosition - _mouseReference);
// apply rotation
_rotation.y = -(_mouseOffset.x) * _sensitivity;
_rotation.x = -(_mouseOffset.y) * _sensitivity;
// rotate
transform.eulerAngles += _rotation;
// store mouse
_mouseReference = Input.mousePosition;
}
}
The issue arises when I try to click on the spheres when the box collider on the brain is on. I end up rotating the brain instead of being able to click the spheres. Is there a way to be able to click inside the collider while also on the collider to rotate the brain?.
Answer by AltaNox · Nov 02, 2020 at 05:46 PM
A way to solve this is to handle both of these behaviors in a single script so you can more easily give precedence to one of them. Otherwise you'd need a third script that oversees both of them to achieve this.
So, my suggestion is to move the code from the Rotation method inside RotateOnMouse to the ClickDetect method (and rename it if you deem it appropriate). After you do that make sure that the Rotation code will execute if and only if you failed to hit a sphere with the raycast. This way, only 1 of the 2 actions can happen at once, and the sphere clicking will have precedence.
@AltaNox So I tried what you had suggested, but I have to move inside the box collider of the brain to click the spheres, the brain collider is intercepting all the clicks. The spheres are children of the brain and are effectively covered by its collider. Is there something I'm missing? Thanks for your help
Answer by madalenaantunessousa · Apr 19 at 01:32 PM
Hello @ASE-Lab! I know this topic is from quite a while but I've run into this exact problem and was wondering if you've managed to solve it and how? Thx!
Your answer
Follow this Question
Related Questions
Why is it so fast to move objects with collider collisions? 1 Answer
Raycast is not returning exact number / Mesh is not exact dimensions 2 Answers
A script that auto generated the box collider around a 3d model 3 Answers
Ray cast affects all colliders in scene if mouse button is not releasesd. 1 Answer