Hello everyone, I was wondering how to highlight an object with a raycast in unity 3d. By highlight, I mean make the object pointed to by a raycast turn a chosen color, then when the ray cast isn’t touching the object, it goes back to it’s normal color.
Hello,
Just set a script that raycast through your scene, then when you hit something you can retrieve the hit object and assign a colour to its material, or you can also set a specific shader that highlight your object, or whatever you want. Then record a reference of your object (tag, name, ID, …). When you hit an object check if it is a new one (It mean different than the previous one), if it is the case just cancel the selected effect on the previous and enable it on the hit one !
using UnityEngine;
using System.Collections;
public class PlayerRayCasting : MonoBehaviour
{
public float distanceToSee;
public string ObjectName;
private Color highlightColor;
Material originalMaterial, tempMaterial;
Renderer rend = null;
void Start()
{
highlightColor = Color.green;
}
// Update is called once per frame
void Update ()
{
RaycastHit hitInfo;
Renderer currRend;
//Draws ray in scene view during playmode; the multiplication in the second parameter controls how long the line will be
Debug.DrawRay(this.transform.position, this.transform.forward * distanceToSee, Color.magenta);
//A raycast returns a true or false value
//we initiate raycast through the Physics class
//out parameter is saying take collider information of the object we hit, and push it out and
//store is in the what I hit variable. The variable is empty by default, but once the raycast hits
//any collider, it's going to take the information, and store it in whatIHit variable. So then,
//if I wanted to access something, I could access it through the whatIHit variable.
if (Physics.Raycast(this.transform.position, this.transform.forward, out hitInfo, distanceToSee))
{
currRend = hitInfo.collider.gameObject.GetComponent<Renderer>();
if (currRend == rend)
return;
if (currRend && currRend != rend)
{
if (rend)
{
rend.sharedMaterial = originalMaterial;
}
}
if (currRend)
rend = currRend;
else
return;
originalMaterial = rend.sharedMaterial;
tempMaterial = new Material(originalMaterial);
rend.material = tempMaterial;
rend.material.color = highlightColor;
}
else
{
if (rend)
{
rend.sharedMaterial = originalMaterial;
rend = null;
}
}
}
}