How should i setup my level for mouse interactivity

Hello

I have a basic level of a bunch of rooms. Each room has a box collider that covers it entirely which i use to update the NPCs room position using on trigger enter/exit.

Now the problem i have is, if i want to select a NPC with my mouse in side this room, it does not fire because the main room collider which is checking for who enters/exits is what is being detected for mouse down and not the NPC that resides inside of it.

My room has:

void OnMouseDown(){
   print("test");
}

And my character has:

void OnMouseDown(){
   print("Selected");
}

But i never receive “Selected” in console. Only “test”.

My character has a box collider as well though it is not “is trigger” as i needed it to collide into things.

This is what my room looks like:

The green box is the box collider set to Is Trigger, it’s there to check what characters enter/exit so i can set their parent correctly for local positions and other stuff i want to implement in the future.

I still need to be able to select the room for the purpose of things like telling a NPC to go a given room which is why i have an OnMouseDown for the room aswell.

What is the best way to fix this problem i am so lost at the moment.

You’ll need to have a second collider in this instance, and have that separate collider listen for the mouse input.

Both the character and the room have a collider but the inner collider (aka my character) won’t detect mouse down because its inside the parent collider (the green box in my screenshot). The mouse down will not fire for character.

Ah, ok. In that case, adjust the Physics settings so that your mouse raycast ignores certain objects based on their Layer, but does not ignore your player’s Layer.

Reference: Unity - Manual: Layer-based collision detection

1 Like

The problem there is, i need to be able to click the room collider as well here is the situations that i am trying to solve:

If an NPC is already selected and i click the room box collider (aka in the image) this tells the NPC to walk to said room - so having the raycast ignore the room collider is not really an option in this case.

If i want to select an NPC which is inside one of these rooms which is the other possible situation that can arise this is when i need to ignore the room collider.

So is it possible to update LayerBasedCollisions with code on the go ? It looks to me like it is a one time set in stone from the editor?

Consider using Physics.RaycastAll. That way you can get all of the colliders that you hit and iterate through them to see which one to activate.

1 Like

Thanks that sounds like the solution i need!