Hi,
I’m sending a Raycast every frame from the mouse position to the camera’s direction and I’m able to get the name from objects it hits properly using layers. But… I can’t seem to find a way to have this Raycast generate an OnTriggerEvent on the colliding object ![]()
I have tried all combinations possible but can’t seem to find a way.
Is it possible and how?
Thanks!
you do know you can call OnTriggerEnter(collider : Collider) right? The problem with a mouse is that it has no collider, but if you substitute a game collider for it, you can call it easy.
Keep what you are over, if it changes, call OnTriggerEnter(collider : Collider), every frame that it is then on it call OnTriggerStay(collider : Collider) and when it exits call OnTriggerExit(collider : Collider).
So yes, you can do this easily.
While Unity doesn’t provide this to you, you could roll your own to achieve the same thing.
void DoRaycastTrigger()
{
RayCastHit hit;
if (Physics.Raycast(..., out hit))
{
hit.transform.gameObject.SendMessage("OnRaycastTriggerHit");
}
}
public class RaycastTriggerScript : MonoBehaviour {
public void OnRaycastTriggerHit()
{
Debug.Log("Hit by raycast");
}
}
Calling the function directly works! Thanks to you both! And thanks for the code KelsoMRK!