- Home /
How do I highlight the color of a material through raycasting?
Hi guys,
I've been pondering over this for way too long. Basically what I'm trying to achieve is to highlight an object when the crosshair detects an object. This is a 3D game, with a crosshair in the middle. If the player walks up to the object within range, the crosshair turn red indicating something can be done with the object. This works perfectly, but now I'd like to actually highlight the object by getting the original material attached to the gameobject and just change its color.
All the tutorials I've come across let you assign a totally different material on raycast, not something I want to do, just get the original color on the material, change it once the raycast detects it and restore the original material (without the color change) when the ray no longer detects the object.
This is what I have so far that works:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
public class Highlights : MonoBehaviour {
public Material highlightMaterial;
Material originalMaterial;
GameObject lastHighlightedObject;
void Update()
{
HighlightObjectInCenterOfCam();
}
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();
}
}
}
As you can see, it assigs a totally different material once it hovers over the detected gameobject, this just needs to be replaced by the orginal material + a color change. But I have no clue how to do that? I assume the public Material highlightMaterial needs to be removed, and something needs to happen inside the HighlightObject function.
Any help would be greatly appreciated!
Answer by xxmariofer · Oct 11, 2021 at 07:50 AM
change
public Material highlightMaterial;
Material originalMaterial;
for
public Color highlightColor;
Color originalColor;
and
originalColor = gameObject.GetComponent<MeshRenderer>().sharedMaterial.color;
gameObject.GetComponent<MeshRenderer>().sharedMaterial.color = highlightColor;
lastHighlightedObject.GetComponent<MeshRenderer>().sharedMaterial.color = originalColor;
Yes!! That worked! I was overcomplicating things! Thank you very much for you time to respond!
Your answer
![](https://koobas.hobune.stream/wayback/20220613044038im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
why gameobject / raycast when dragged is not accurately following the touch position in unity 0 Answers
Unity 2021 LTS: IPointerEnterHandler is broken 1 Answer
why is part of my script being ignored? 2 Answers
GameObject facing hit.point doesn't always work 1 Answer
How to Pick up Gameobject 1 Answer