- Home /
How do I highlight an object when the player is near it
I have been working on a horror game called 'Stalk' for a while now. I am trying to implement a way of picking up items, but there is a problem. I am trying to highlight the object when the player is near it, but because the checking would need to be in the update function, setting the current color/material to a color and then changing it's color wouldn't work because the variable would then be set to yellow, making the object stay yellow. Is there a way to make this work?
Answer by pickle chips · Aug 01, 2013 at 02:28 AM
You could just store the original colour in a variable, and then change it back to that once the player is no longer near. For example:
private Color originalColor;
void Start() {
originalColor = renderer.material.color;
}
void Update() {
if (distance < range) { //Checking if player is near enough
renderer.material.color = Color.yellow;
}
else {
renderer.material.color = originalColor;
}
}
don't forget to show him how to set the distance, using X or Z coords.
public Transform playerChar, objToAffect;
protected int distance = objToAffect.position.x - playerChar.position.x;
Yes, but it seemed the question was more oriented towards the colour changing, so i left that out :p I guess it would have been a good idea to include it though, thanks for adding it in!
And in case you want the distance on all axes, you can use Vector3.Distance()
This actually doesn't work as I am using this to see which object will be picked up and put in the hand, and I am using an array, and assigning all pickable object in an array, and finding which one is the closest.
...I don't know how that answer doesn't work with you? That script can be attached to any objects with mark "pickable"(you can assign manually or by some boolean).By the way, if what you need is finding closest Object in an array and highlight it,so here you are:(this script placed in player gameObject)
public GameObject[] objarray;//this array is manually assigned
void Update()
{
float closestdistance;
GameObject closestgameObject;
for(int i=0;i<objarray.Length;i++)
{
float distance = Vector3.Distance(transform.position,objarray[i].transform.position);
if(distance<=closestdistance)
{
closestdistance = distance;
closestgameObject = objarray[i];
}
}
closetgameObject.renderer.material.color =...//anything you want
}
Actually above code only show you the "closest" object, it means just one, so i think maybe you don't want something like that. Try this code ins$$anonymous$$d:
public GameObject[] objarray;
public float rangefinditem=10f;//items will change color if its distance to player is <= 10
void Update()
{
for(int i=0;i<objarray.Length;i++)
{
float distance =Vector3.Distance(transform.position,objarray[i].transform.position);
if(distance<= rangefinditem)
objarray[i].renderer.material.color =...//anything you want
else
objarray[i].renderer.material.color =...//original color
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613111320im_/https://answers.unity.com/themes/thub/images/avi.jpg)