I have a use-case where I have an object with a Collider2D and a Kinematic RigidBody2D in order to do Kinematic movement.
However, I also want to be able to select/hover that object with a OnMouseDown/OnMouseEnter/etc, using a larger collider than I would use for the kinematic movement.
My current attempt at a solution is to have Physics-related scripts/rigidbody2d/collider2d as the top level game object, and to have a child object, with a larger collider2D which will respond to OnMouseX events.
In order to ignore OnMouseX events on the parent, I'm putting it into its own user_ignore_raycasts
layer (I need it as its own layer for the layer collision matrix). I'm also putting the child in its own user_use_raycasts
layer.
I'm passing that into a script that sets MainCamera's event mask:
using UnityEngine;
[RequireComponent(typeof(Camera))]
public class CameraEventMasker : MonoBehaviour
{
[SerializeField]
private LayerMask cameraEventMask;
// Start is called before the first frame update
void Awake()
{
GetComponent<Camera>().eventMask = cameraEventMask;
}
}
What I notice is that OnMouseX events on the child will work IF the RigidBody2D on the parent is deleted...however, if the RigidBody2D is there, I won't receive any events. Anyone know why this is the case, and if there are other workarounds or solutions?