I recently encountered a unique problem while making a game. As you can see from this screenshot, I have a thing acting like a mouse, on a surface gui (the math was though don’t ask). However now I need to get a ball to tween to said location.
The location is that square inside the yellow square. The problem here is that it moves and I’m not entirely sure how I would calculate coordinates for it. As you see the ball does not move to the position (I got it semi close, but it only works for the center).
Here is the math sample I’m working with to attempt to calculate this. Note that it is flush to the Z value so I don’t need to do any math with that.
This could sort of solve the issue. I would have done this myself except for the fact that the mouse is a sudo mouse. It doesn’t align up with the real mouse as seen here:
The square is based on the mouses position. I had to do some mathematics to decrease the size and make it more sensitive though, as well as translate the position, seeing as I wanted it to center in the screen, and not be off.
This is the code I’m using for that:
You can use Camera:ScreenPointToRay (or ViewportPointToRay) to translate a 2D screen position to a unit ray originating from your camera! Using this knowledge, you can get the position on 3D space any distance away from your character using desiredPosition = CAMERA.CFrame.Position+CAMERA:ViewportPointToRay(MOUSE.X,MOUSE.Y).Direction*distance
Or in this case, replace MOUSE.X and MOUSE.Y with the AbsolutePosition components of your faux mouse
EDIT: oops, used the wrong method and forgot to include the Direction of the ray. Should work now