How do you highlight an object pointed to by a raycast unity 3d?
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.
Answer by Vicarian · Jun 22, 2017 at 02:57 PM
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;
}
}
}
}
Thanks so much for the response @Vicarian , unfortunately though, this still didn't work. The raycast still hits the object and changes the object's color, but when the raycast is not touching the object anymore, the object still does not go back to it's normal color.
@AtGfx I tried that method and everything still stays one color.
$$anonymous$$ight have to make a new material then. I was hoping to avoid that, but it's not too difficult.
I've updated my original answer.
I've added code to my original answer to handle an edge case, where you pass immediately from one renderer to another, without your raycast going null
The answer was updated. I didn't have to change anything else in the scene, but you do have another camera in the scene that doesn't need to be there.
Answer by AtGfx · Jun 22, 2017 at 02:44 PM
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 !
Thanks @AtGfx for your response, unfortunately, I've already tried this and this method does not work.