- Home /
Check for an instantiated object farthest from player?
I'm trying to make a script that when the player moves a certain distance from any gameobject with a certain tag, SetActiveRecursively(false), but only deactivate the object farthest away from the player.
e.g (P is player)
| || | | P | | |
<--
when the player moves to here
| || P| | | | |
set this to false
| ||P | | | | X |
How would i go about doing this?
what do you mean "only deactivate the object farthest away from the player".. You only want ONE object to deactivate? Or each one once it's far enough away?
One other question.. when you get closer to that object again, should it turn back on? or once it's deactivated, it's gone for good?
OR, do you mean that only the furthest object should be turned off at any given time?
when you get closer it should turn back on, that's why i was using SetActiveRecursively rather than just destroying it.
Answer by Seth-Bergman · Nov 07, 2012 at 02:28 AM
OK, well, if you are trying to keep track of All the objects of a given tag, first you will need an array of all objects with that tag.. then you will cycle through the array, checking the distance of each from the player, and setting them accordingly:
var myObjects : List.<GameObject> = new List.<GameObject>();
var maxDistance : float = 20.0;
function Update(){
//GetObjects();
for (var obj in myObjects)
{
if(Vector3.Distance(transform.position,obj.transform.position) > maxDistance && obj.active){
obj.SetActiveRecursively(false);
Debug.Log("Distance is: " + Vector3.Distance(transform.position,obj.transform.position));
}
else if(Vector3.Distance(transform.position,obj.transform.position) <= maxDistance && !obj.active)
obj.SetActiveRecursively(true);
}
}
function GetObjects(){
var tempObjects = GameObject.FindGameObjectsWithTag ("SomeTag");
for(myObj in tempObjects){
if(!myObjects.Contains(myObj))
myObjects.Add(myObj);
}
}
you could uncomment the first line in Update, to make this work, but it is more efficient to only call this relatively expensive function as necessary.. so, if you can instead call the function "GetObjects()" only once each time you INSTANTIATE a new object, that will be more efficient.. for now, however, go ahead and un-comment the line in Update, if you like..
EDITED: forgot to mention,this script would be attached to the player.. using a list gives us the best control, so this should work now I think.. The debug should tell you the distance if it turns anything..
Thanks for the reply, when i play however, the size of the array immediately goes to 0. All the prefabs that i have instantiated have the same tag, which is the one i'm checking for in the start function.
EDIT: I tried putting it in the update function, which means now the objects deactivate, but they all seem to deactivate no matter where the player is, even if i mess around with the maxDistance variable.
no prob, let me update my answer above to make it clearer..
O$$anonymous$$, edited.. Basically, it can be costly using GameObject.FindGameObjectsWithTag EVERY frame, and it's not really necessary here since you can call it each time you instantiate a new object
(AFTER the new object is instantiated, you call the function to update the list)
for now though, just un-comment the first line of Update, and try it that way
If it's in update it still deactivates all of them, but when it isn't, i noticed that(because the array size is always resetting to 0 when i play) if i manually add something to the array, no matter the max distance(even at something like 100000), it deactivates whatever object is in the array.
O$$anonymous$$, maybe FindGameObjectsWithTag only finds ACTIVE objects then.. let me change one more thing...
O$$anonymous$$, have a look now, I think this should do it.. ..make sure the script is attached to the player (forgot to mention that before), and if it still turns something off, it should log the distance
YES! That works! Thanks a heap you've been very helpful! (i orignally thought it wasn't working but i forgot to resave the script >_<)
Your answer
Follow this Question
Related Questions
How to do something when an object is in the player sights. 2 Answers
Distance to object and rotation lock 1 Answer
How to use tag for all players in the Instantiate. 0 Answers
help loading level after destroying objects 1 Answer
Camera Problem 1 Answer