Search Unity

Raycast, FPS, highlighting object on the cursor.

Discussion in 'Getting Started' started by Tanchizu, Aug 17, 2017.

  1. Tanchizu

    Tanchizu

    Joined:
    Jul 26, 2017
    Posts:
    1
    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. :p

    Please help =)
     
  2. eXonius

    eXonius

    Joined:
    Feb 2, 2016
    Posts:
    207
    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.

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.Rendering;
    6.  
    7. public class Highlights : MonoBehaviour {
    8.  
    9.     public Material highlightMaterial;
    10.     Material originalMaterial;
    11.     GameObject lastHighlightedObject;
    12.  
    13.     void HighlightObject(GameObject gameObject)
    14.     {
    15.         if (lastHighlightedObject != gameObject)
    16.         {
    17.             ClearHighlighted();
    18.             originalMaterial = gameObject.GetComponent<MeshRenderer>().sharedMaterial;
    19.             gameObject.GetComponent<MeshRenderer>().sharedMaterial = highlightMaterial;
    20.             lastHighlightedObject = gameObject;
    21.         }
    22.  
    23.     }
    24.  
    25.     void ClearHighlighted()
    26.     {
    27.         if (lastHighlightedObject != null)
    28.         {
    29.             lastHighlightedObject.GetComponent<MeshRenderer>().sharedMaterial = originalMaterial;
    30.             lastHighlightedObject = null;
    31.         }
    32.     }
    33.  
    34.     void HighlightObjectInCenterOfCam()
    35.     {
    36.         float rayDistance = 1000.0f;
    37.         // Ray from the center of the viewport.
    38.         Ray ray = Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f));
    39.         RaycastHit rayHit;
    40.         // Check if we hit something.
    41.         if (Physics.Raycast(ray, out rayHit, rayDistance))
    42.         {
    43.             // Get the object that was hit.
    44.             GameObject hitObject = rayHit.collider.gameObject;
    45.             HighlightObject(hitObject);
    46.         } else
    47.         {
    48.             ClearHighlighted();
    49.         }
    50.     }
    51.  
    52.     void Update()
    53.     {
    54.         HighlightObjectInCenterOfCam();
    55.     }
    56. }
    57.  
    58.  
    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.
     

    Attached Files:

  3. Vinayak-VC

    Vinayak-VC

    Joined:
    Apr 16, 2019
    Posts:
    60
    Worked like Charm
    Thanks