-
What do you want to achieve? Keep it simple and clear!
I want to grab the closest Player or Character in a certain proximity to the cursor. In other words I want a way to measure the distance between the cursor and the Player or Character. I need this in order to make attacks for my game that track onto the player. -
What is the issue? Include screenshots / videos if possible!
I tried using mouse.hit in order to get the distance between the cursor and Character but it didn’t work out the way I wanted.
Hi, do you mean the character’s position on your screen relative to your mouse position on your screen?
Not world space?
If so you could use:
local x, y, z, isVisible = workspace.CurrentCamera:WorldToScreenPoint(the position, must be a Vector3)
To get the position and a bool saying if it’s visible on screen, for comparison with your mouse’s current position. The Z component is depth in studs.
If you’d like to learn more about this you could have a look here.
Thank you for the reply! I actually figured out how to do this with mouse.hit but another issue came up when I did it. In this case the player is holding planning to attack the 6th Dummy. But because the first dummy is technically closer to the mouse this results in the detected character ending up being the wrong one. Same thing goes if the player tries to aim the mouse in the air close to the player.
@DroneMiner967_Gaming’s most recent response is how you would do that. With that function you can get the screen position of the thing (x,y) and simply compare that distance to the cursor position.