Translating 2d space to 3d space for tween locations

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).
U90mJie92a

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.

local xLocation = ((workspace.throwSquare.FrontFrame.mousePosition.Position.X.Scale * workspace.throwSquare.Size.X) + workspace.throwSquare.Position.X)-1
					local yLocation = ((workspace.throwSquare.FrontFrame.mousePosition.Position.Y.Scale * workspace.throwSquare.Size.Y) + workspace.throwSquare.Position.Y)-3.7
					tweenService:Create(cloneBall2, TweenInfo.new(4, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0), {CFrame = CFrame.new(xLocation, yLocation, workspace.throwSquare.CFrame.Z) * CFrame.new(0, 0, 40)}):Play()

(Apologies in advance that the code does not fix nicely.)

Any help would be appreciated! Thanks!

1 Like

Not really sure, if I got the problem right, but couldn’t this just get simply solved by

local mouse = game.Players.LocalPlayer:GetMouse()
mouse.Hit.p --returns Vector3 value of position of the mouse.

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:

Notice how the real mouse is offset from the screenUI mouse. It’s due to some weird math I’m doing.

On what thing is based the position of the yellow square? Is it some math, or just position of your mouse? How is player able to move the square?

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:

tweenService:Create(workspace.throwSquare.FrontFrame.mousePosition, TweenInfo.new(.005, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0), {Position = UDim2.new((mouse.X/(workspace.throwSquare.FrontFrame.AbsoluteSize.X-500))-1.535, 0, mouse.Y/(workspace.throwSquare.FrontFrame.AbsoluteSize.Y-500)-3.25, 0)}):Play()

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 :slight_smile:

EDIT: oops, used the wrong method and forgot to include the Direction of the ray. Should work now

2 Likes