I am trying to get a ray to trigger an OnTrigger event when it hits an object (to detect mouse clicks). There are hosts of threads asking about how trigger events work, and as far as I can tell, I’m doing everything correctly.
Here is what I have set up:
- My game object has an attached RigidBody and a Collider
- The Collider is set to be a Trigger
- In Project Setting → Physics, rays are *not configured to ignore triggers
- The collision matrix in the settings enables all collisions
- The collider’s parent object is not on the Ignore Trigger layer, nor is the ray (at least, I don’t think so).
I create the ray with the following code, in a script attached to the main camera:
if (Input.GetMouseButtonDown (0))
{
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
Physics.Raycast (ray, out hit);
if (hit.collider != null)
{
print(hit.collider.ToString ());
}
}
When the user clicks on the object with the collider, the ray *does hit it. The print statement verifies that the ray is running into the collider as expected.
Here is a snippet of the script, attached to the same object as the collider, that should be called on the event:
void OnTriggerEnter(Collider other)
{
selected = true;
print ("In trigger.");
}
Any ideas? I’ve been beating my head on this one for a while now!