- Home /
Question by
ProfMonkey07 · Aug 03, 2020 at 04:22 AM ·
c#transformarraymousepositionbuilding
get nearest game object
for a build script I need to get the GameObject in an array thats x and z are closest to the mouse y and z and store them in a variable, could someone please help?
Comment
Answer by rufopufo · Aug 03, 2020 at 07:01 AM
Hi there,
You can use Vector3.Distance(). Just by looping around all the array and saving the nereast distance.
In order to use it, you should compare both objects transform.position.
Example: Vector3.Distance(myObject.transform.position, gameObjectsInArray[i].transform.position);
Hope it helps :)
Your answer
Follow this Question
Related Questions
Array foreach problem 1 Answer
problem moving a prefab object in script (c#) 0 Answers
Distribute terrain in zones 3 Answers
How to transform gameObjects in array? 1 Answer
Setting position of a transform? 2 Answers