Checking whether an object is being hit by a ray

I know how to check if the ray hits anything.
But does it work the other way?
Something like collisions:

void OnCollisionEnter(Collision other)
{
if (other.gameObject.CompareTag (“Tag”)

So I need something like this:
void BeingHitByTheRay
{
if(RaysName = Ray)
do something
}

How do I achieve that?

I’m going through my hexes by shooting a ray from camera into them. If a hex IS hit by a ray it is highlighted. If it is NOT hit – not highlighted.

So the script ‘BeingHitByRay’ is on my hexes prefabs, not on the Ray itself. How should I alter it? Thanks in advance :slight_smile:

It sounds like you want simple mouse over events. You can let Unity handle that with EventSystem and PhysicsRaycaster.

  • If you haven’t got an EventSystem in your scene, add one from GameObject/UI/Event System.
  • If you haven’t got a PhysicsRaycaster in your scene, select your camera and Add Component Physics Raycaster.

On the BeingHitByRay script, implement IPointerEnterHandler and IPointerExitHandler.

// Use namespace...
using UnityEngine.EventSystems;

// Implement interfaces...
public class BeingHitByRay : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
    // Handle event
    void IPointerEnterHandler.OnPointerEnter(PointerEventData eventData)
    {
        // SetColor(highlighted);
    }

    // Handle event
    void IPointerExitHandler.OnPointerExit(PointerEventData eventData)
    {
        // SetColor(notHighlighted);
    }
}

And you should be good to go.


If you wanted to tell the object being hit (like letting the other object respond to a bullet hit for example) you can use SendMessage (Old system, easy to use) or ExecuteEvents.Execute (New system, typesafe, you define and implement interfaces). SendMessage can pass zero or one arguments and it cannot be null.

using UnityEngine;

// Put this on something looking directly at the cube, within 10 meter distance
public class RaycastExample : MonoBehaviour
{
    // These methods will be called on the object it hits.
    const string OnRaycastExitMessage = "OnRaycastExit";
    const string OnRaycastEnterMessage = "OnRaycastEnter";

    GameObject previous;

    void Update()
    {
        RaycastHit hit;

        if (Physics.Raycast(transform.position, transform.forward, out hit, 10f))
        {
            GameObject current = hit.collider.gameObject;
            if (previous != current)
            {
                SendMessageTo(previous, OnRaycastExitMessage);
                SendMessageTo(current, OnRaycastEnterMessage);
                previous = current;
            }
        }
        else
        {
            SendMessageTo(previous, OnRaycastExitMessage);
            previous = null;
        }
    }

    void SendMessageTo(GameObject target, string message)
    {
        if (target)
            target.SendMessage(message, gameObject,
                    SendMessageOptions.DontRequireReceiver);
    }
}

And then you have some kind of receiver.

using UnityEngine;

// Put this on the cube.
public class RaycastReceiver : MonoBehaviour
{
    // These methods will be called by RaycastExample
    void OnRaycastEnter(GameObject sender)
    {
        print("Hit by " + sender.name);
    }

    // These methods will be called by RaycastExample
    void OnRaycastExit(GameObject sender)
    {
        print("No longer hit by " + sender.name);
    }
}

Physics.Raycast is your friend here! If you need to check if it hits SPECIFIC objects, make a public LayerMask property and define that in your editor, so that it only affects objects on the layer you need it to.

Note: if you’re trying to hit a trigger with your raycast, make sure under Edit > Project Settings > Physics, you have “Raycasts Hit Triggers” checked.