- Home /
Getting the closest Vector3 to the mouse position
I've made a function that places vectors in a list making a grid, and I want to somehow highlight which vector or grid that is selected (When the mouse is hovering over it). I tried putting the Vectors in a list and then for each vector I would do the "if distance is less than closest, closest = distance" and than worked when I only hade a few grid but when I made the map bigger it became a problem.
I then started thinking about making like a OverlapSpherethat would get only the closest vectors and then find out which one of those vectors are the closest. But the thing is I don't know how to make the OverlapSphere only check those vectors. (I can't really make them into gameobjects due to performance on larger maps i think, not sure about that though).
If you have any other ideas please feel free to tell me. Thanks.
Answer by Jwizard93 · Jul 11, 2017 at 07:44 PM
You could try making a 2D array instead of list where the indexes represent the Vectors actual position in the grid. That way the mouse position would put you "inside of a box on the grid" and you would just test the four corners for the closest one.
Does that make sense?
Yeah, thanks. I will probably try that tomorrow, sounds great
Could you try to explain it a bit $$anonymous$$ore. I just realised i dont understand it
I finally got it to work doing what you told me. Thank you!
Sorry I wasn't there to better explain earlier. But hey I can't say that I would have figured it out any faster than you. Good Job!
Answer by Cornelis-de-Jager · Jul 11, 2017 at 11:29 PM
There is a InBuilt Unity Function that you might want to have a look at.
void OnMouseEnter () {
// Mouse is now over so change the colour/highlight object
}
void OnMouseExit () {
// Return to default
}
I can't see that working since that world only work on gameobjects. I could be wrong though
Your answer
Follow this Question
Related Questions
geting the closest object from a array 2 Answers
Move an object towards closest enemy. 1 Answer
Sorting an array of vectors in javascript 1 Answer
Vector math problem 2 Answers
Drag/Move an object perpendicular to starting point 1 Answer