Hiya,
I am working with some highlight code provided from some sample I recieved from someone up here and am running into a problem where multiple materials are attached to an object, for example a door.
This door has two materials, a glass material and a door material. The glass material is in the 1st slot and the door material is in the 2nd slot. When I make the call to highlight the mesh only the glass material is executing the highlight.
So, I was looking at a way to essentially iterate through all the materials associated with the mesh, store the base color of each and change the color but only for the particular instance of the material that is associated with this mesh. I read and was thinking about sharedMaterials but that seemed to not be the way to go.
Does anyone have some ideas or better ways to do highlighting?
Here is the code I am using:
private var overObject : GameObject;
private var baseColor : Color;
private var highlightFactor : float = .4;
function Update () {
// Get RayCast Hit
var r : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var hit : RaycastHit;
// Check Hit Make Sure No Current Object
if (Physics.Raycast(r, hit)) {
if (overObject == null) {
// Set Over Object Highlight
overObject = hit.transform.gameObject;
this.highlightCurrentObject();
// Left Orig Over Object Over New One
} else if (hit.transform != overObject.transform){
// Remove Highlight Of Old Object
this.restoreCurrentObject();
// Store New Object Highlight
overObject = hit.transform.gameObject;
this.highlightCurrentObject();
}
} else {
// Not Over Any Interact Object Restore Highlight (if previously stored)
this.restoreCurrentObject();
}
}
function highlightCurrentObject() {
// Make Sure Object Is Interact
if (overObject.tag == "Interact"){
// Get Highest Level Renderer Base Color
var rend : Renderer = overObject.GetComponentInChildren(Renderer);
baseColor = rend.material.GetColor("_Color");
// Generate New Color Apply
var newColor : Color = Color(baseColor.r + highlightFactor, baseColor.g +highlightFactor, baseColor.b + highlightFactor, baseColor.a);
rend.material.color = newColor;
}
}
function restoreCurrentObject() {
// Ensure Object Is Had Able To Ineract
if (overObject != null) {
if (overObject.tag == "Interact"){
// Get Highest Level Renderer Reset To Base Color
var rend : Renderer= overObject.GetComponentInChildren(Renderer);
rend.material.color = baseColor;
// Clear Object
overObject = null;
}
}
}
Thanks for the help!
Regards,
– Clint