- Home /
Custom layer to be ignored by OnMouseOver ?
Hello,
I need to create a layer that will be ignored by OnMouseOver but it won't be ignored by Raycasts. Is this possible ?
My problem comes because of the following scenario :
I have a planet and I'm using a sphere collider to encapsulate all of the planet + landscape. However some landmarks are interactive, but because the sphere collider is bigger the OnMouseOver function is no longer called.
I need the sphere collider for various actions which involve raycasts. If I move the planet to the IgnoreRaycast layer, the landmarks are clickable but I bellive I cannot raycast to that layer (or I didn't use the right mask for the IgnoreRaycast layer ).
Any tips ? Anyone ?
It depends on how you are using On$$anonymous$$ouseOver and what Layers you are already using. Are you using the Eventsystem object? Are you manually writing your raycasts via Phyics.Raycast() to emulate On$$anonymous$$ouseOver, or are you using the On$$anonymous$$ouseOver Function that comes with the $$anonymous$$onoBehavior?
After fiddling with all these options, my personal preference is using the EventSystem as it gives you so much control over all sorts of raycasting (from the camera) and access to all the fun pointer events (including OnPointerEnter, which is similar to On$$anonymous$$ouseOver but works for both PC and Tablet).
Do you ever need to use mouse events on the Planet's collider itself or are they raycasts independent from the camera's position? If they are independent then you can easily place the planets collider on an entirely different collision layer and just have your On$$anonymous$$ouseOver Raycaster mask that layer
(if via the EventsSystem it'd be a layer you remove on the GraphicsRayCaster Component that you would have to attach to your camera)
the exact answer I can give just depends on how you're doing it in your game. can you show some code and what layers you have?
I am in a very similar situation, I would like my raycasts for On$$anonymous$$ouseEnter ignore some colliders.
I'm using the EventSystem, and I would like the On$$anonymous$$ouseEnter ignore the collider of the player object (on the Player layer). I cannot turn off the collider of the player, because that would cause all kinds of problems.
I think I would need what you mentioned, remove the layer from the GraphicsRaycaster component on the main camera, but I don't understand what you mean. Could you explain it in a bit more detail?
EDIT: O$$anonymous$$, now I understand what you mean: add a raycaster to the main camera (type depends on your game), and set the event mask to whatever layers I want to ignore. This would be nice, except raycasts don't work well with a Character Controller script, they still get blocked. Anyway, thanks for the info!
if that script were to be on a UI element like a button, then you would need a Graphics raycaster on the parent canvas with the event mask set to the same layer the script is set to. however if the script is on a 3d game object ins$$anonymous$$d you need a Physics Raycaster on the Camera (its a different component). Theres also a Physics 2D raycaster as well for 2d elements
in both cases you'll need an Event system gameobject, which it sounds like you have. and finally you need to have your script, your character controller, listen to the right calls. When using the Event system you will want to use OnPointerEnter ins$$anonymous$$d of On$$anonymous$$ouseEnter. Look into the Event System documentation to learn more about it. however the documentation is lacking a little in example code as far as the pointer handlers are concerned so this should help you start out
using UnityEngine;
using UnityEngine.EventSystems;
public class CharacterController: $$anonymous$$onoBehaviour, IPointerEnterHandler
{
public void OnPointerEnter (PointerEventData eventData)
{
Debug.Log("entered: " + gameObject);
}
}
if you want you raycasts to ignore certain colliders make sure they are on separate layers and have the Phycis/Graphics Raycaster only focus on specific layers. and you can use code to swap that out whenever you want.
Your answer
Follow this Question
Related Questions
function OnMouseOver() ignore selected raycast 1 Answer
ignore raycast layers problem 1 Answer
How do I use layermasks? 9 Answers
Layer mask doesn't work... 1 Answer
Use OnMouseOver or Raycasting while also displaying a camera texture. 0 Answers