Vector3.Distance - Triggering an action on distance
Hello All! I`m working on a project of mine, and i feel kind of lost. Here is the scenario: I have a player who walks around, and some Cube objects around him. I want to measure the distance between the player and the objects, and when the player gets close enough to any of the objects then trigger a popup window saying "Press X button to do Y".
Now on every object i have a - public float minDist; and also - public float currDist; Obviously I`m using: Vector3.Distance to measure the distances between the player and the objects, and set currDist by it.
When currDist is lower when minDist I want the trigger to happen, but for some reason i found that only 1 object sets the trigger and not all of them..
Any ideas please?
Code:
public float minDist;
public float currDist;
void Update () {
currDist = Vector3.Distance (transform.position, Player.transform.position);
if (currDist < minDist) {
//Enable Trigger
}
else if (currDist > minDist){
//Disable Trigger
}
}
Answer by HenryStrattonFW · Feb 01, 2017 at 01:24 PM
This will be because all of the items are running at the same time, and whilst one object will run, enable the trigger, and carry on, another item will immediately say "I'm too far away" and disable the trigger again.
IO would suggest storing a value in the objects, when they enable the trigger, something like "ResponsibleForTrigger" and only let them disable the trigger, if this flag is set. In other words any object can enable the trigger, but only an object that has enabled the trigger can disable it.
Hope this makes sense.
First of all Thank you @HenryStrattonFW for the quick support, i think i understand your point, but i cant get it to work. $$anonymous$$aybe can you provide a small code for it?
$$anonymous$$any thanks!
@HenryStrattonFW finally i succeed! Thank you very much! Understood your point very clearly :)
Hey, sorry answered on my lunch break so wasn't able to respond with an example, but glad to hear you worked it out. Best of luck with your project.
Your answer
Follow this Question
Related Questions
Don't trust "Vector3.Distance"? 1 Answer
Error with vector3.distance 0 Answers
Lerp to a fixed distance 0 Answers
Distance between two objects, from the view of the camera. 0 Answers
Calculating distance is not working 1 Answer