Need help getting randomly moving particles to head to the nearest of 4 coordinates.
Hey guys, I am using c# in the unity engine.
I have created an environment and in the environment i have random Gameobjects moving around randomly, I need to get on the press of the button get them to move to the closest set of coordinates. Their targets also have a set of special tags "goal"
Currently I have got it so they will head to one of the coordinates at a time using Vector3 and transform but I have had no luck getting further than that. Any suggestions would be massively appreciated.
Thanks!
Answer by BaldBeardedMonk · May 06, 2017 at 09:10 PM
you can use Vector3.distance for it. Check the distance of every goal from the gameobject. and move it towards the smallest one.
float dist = Vector3.Distance(other.position, transform.position);
The below code too can be used. it is equivalent of using Vector3.distance
float dist =(transform.position-targetPosition).magnitude
you can use a foreach loop to select one gameobject tagged as 'goal' at a time and then compare the transform.position for each goal gameobject to the random gameobject.
GameObject[] GO = GameObject.FindGameObjectsWithTag ("goal");
foreach (GameObject G in GO)
and inside this foreach check for the minimum distance.
Hope this helps. :)
Your answer
Follow this Question
Related Questions
GameObject not changing position in PlayMode although the Transform changes 1 Answer
Don't trust "Vector3.Distance"? 1 Answer
Trying to Generate Different Random Values for Position of Game Object Instances [C#] 1 Answer
Set new default position each time? 1 Answer
Vector3.MoveTowards moving transform 0 Answers