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?.

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.

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!