Raycast, FPS, highlighting object on the cursor.

Hi.

I try to highlight object when the FPS controller look on it but i fail all the times. I have found i can do that with the Raycast (i think…) but i’m very bad on script and i can’t use all of them i found on internet. So, highlighting an object with a raycast looks like pretty hard actually. :stuck_out_tongue:

Please help =)

Hello!

You can use a raycast to find out what object your “FPS controller look on” but then you need to use a material or a shader to actually create the highlight on the object.

By “highlight object when the FPS controller look on it” I assume you mean you want to highlight an object when it’s in the center of the screen. To do this you can shoot a ray from the camera and see what object it hits (remember the object must have a collider otherwise the raycast will pass through).

Here is some code for finding the object the camera is looking at and then changing the material to a specific material you choose to use as your highlight material. However it will highlight the whole object. Highlightning only the outline is a lot more involved and requires custom shaders and whatnot.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;

public class Highlights : MonoBehaviour {

    public Material highlightMaterial;
    Material originalMaterial;
    GameObject lastHighlightedObject;

    void HighlightObject(GameObject gameObject)
    {
        if (lastHighlightedObject != gameObject)
        {
            ClearHighlighted();
            originalMaterial = gameObject.GetComponent<MeshRenderer>().sharedMaterial;
            gameObject.GetComponent<MeshRenderer>().sharedMaterial = highlightMaterial;
            lastHighlightedObject = gameObject;
        }

    }

    void ClearHighlighted()
    {
        if (lastHighlightedObject != null)
        {
            lastHighlightedObject.GetComponent<MeshRenderer>().sharedMaterial = originalMaterial;
            lastHighlightedObject = null;
        }
    }

    void HighlightObjectInCenterOfCam()
    {
        float rayDistance = 1000.0f;
        // Ray from the center of the viewport.
        Ray ray = Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f));
        RaycastHit rayHit;
        // Check if we hit something.
        if (Physics.Raycast(ray, out rayHit, rayDistance))
        {
            // Get the object that was hit.
            GameObject hitObject = rayHit.collider.gameObject;
            HighlightObject(hitObject);
        } else
        {
            ClearHighlighted();
        }
    }

    void Update()
    {
        HighlightObjectInCenterOfCam();
    }
}

Copy the code into a new script named “Highlights” and then drag and drop a Material from your asset folder to the public field named “highlightMaterial”. Then just attach the script to any GameObject and it should start changing the material on the object you look at. I attach a simple example scene you can look at in case you get any problems.

To run the example project, unzip it to a folder and then open the project folder with Unity. Double click the asset named “Scene” to load the scene and click play.

3187537–243284–HighlightTest.zip (324 KB)

6 Likes

Worked like Charm
Thanks