Is it possible to get the position of the mouse and convert it into 3D Position?

  1. What do you want to achieve?
    Whenever I click anywhere in the Map Background UI, I want to fire the remote event with Mouse Position in it, so then, the CFrame Value will be the exact same position but in 3D, while doing that, this Red Dot will move there.
    image

(Before you ask, yes, I’m aware that there is Vector3 Value, but, I would like to use this one for some reasons.)

  1. What is the issue?
    However, there is ONE PROBLEM THAT I COULDN’T FIX, and, that is, whenever I tried to click below it, it moves down, that looks okay, yeah? But, If I click above it, it just gets slow and it’s not even moving up, you will see what I meant.
    https://gyazo.com/2fe9c7bdb97b76fc6a10b56c7c5a891d

  2. What solutions have you tried so far?
    I tried to find any solutions about this, but, there doesn’t seem to be what I’m looking for, and, I’m stuck here trying to figure out how to fix this. So, please, if you have a solution for me, I would appreciate it!

1 Like

Could you share the code you have so far?

Once you have the pixel location of the mouse, a useful method is given by the camera instance to convert that into a ray in 3D. Here’s the docs Camera:ViewportPointToRay

I’ve actually encountered something very similar to you and all I have to say is you need a couple hacky methods in order to do this successfully.
First off you need a part the size of the entire map. For now lets refer to this part as MapScale. It doesn’t have to be visible or interfere with game play at all. just turn off collisions and cantouch. I conveniently have this function already made as I said I’ve done this myself:

local function relative_pos(X,Y,Gui_Object)
	local newAirstrikeIcon = airStrikeIcon:Clone()
	local absoluteSize = Gui_Object.AbsoluteSize
	local absolutePosition = Gui_Object.AbsolutePosition
	local midPointX = (absoluteSize.X/2)+absolutePosition.X
	local midPointY = (absoluteSize.Y/2)+absolutePosition.Y

	local preXCoord = -X + midPointX
	local preYCoord = -Y + midPointY


	local xCoord = preXCoord/absoluteSize.X
	local yCoord = preYCoord/absoluteSize.Y

	newAirstrikeIcon.Position = UDim2.new(0,X-absolutePosition.X,0,Y-absolutePosition.Y)
	newAirstrikeIcon.Parent = Gui_Object
	placeAttachment:FireServer(xCoord,yCoord)
end

This should be in the local script. What this function does is take the size of the map gui and converts the mouses X and Y coordinates to a point on MapScale. Then fires an event to the server. Once you’re on the server script this line of code with get the exact position on part

local coordConversion = Vector3.new(coordX*baseplate.Size.X,0,baseplate.Size.Z*coordY)

That’s what I had to do to solve it for myself. I originally wasn’t going to share this but I didn’t want you to go through the same multi day ordeal as I did.

4 Likes

The parts in the code that have airstrike in them you can ignore.

Of course, this is the code I have so far, could you figure out what’s wrong with this? I’m not familiar with this 2D stuff, to be honest :sweat_smile:

 -- // Server Script // --
local function Move(Player, CFrame_Value, Mouse_Position)
	if CFrame_Value then
		local Speed = CFrame_Value:FindFirstChild("Speed")
		if Speed then
			local Vector = Vector3.new(Mouse_Position.Y, 0, 0) -- // For testing purposes
			local Distance = (CFrame_Value.Value.Position - Vector).Magnitude -- // Tween Speed

			TweenService:Create(
				CFrame_Value,
				TweenInfo.new(
					Distance / Speed.Value,
					Enum.EasingStyle.Linear,
					Enum.EasingDirection.Out
				),
				{Value = CFrame.new(Vector)}
			):Play()
		end
	end
end

Move_Event.OnServerEvent:Connect(Move)
-- // Client Script // --
Map_Background.InputBegan:Connect(function(Input)
	if Input.UserInputType == Enum.UserInputType.MouseButton1 then
		if Selected_Dot then
			local Position = UserInputService:GetMouseLocation()
			Move_Event:FireServer(Selected_Dot, Position)
			print(Mouse_Position)
		end
	end
end)

Alright, I’ll try it and let you know if it works or not! :smile:

1 Like

Well, I’ve just tried your code, it’s actually working pretty good, but, although, there are issues, so, I had to edit it myself a little lol

https://gyazo.com/128ff42cf1cf27fdc5944f42bf5f7a5a
https://gyazo.com/527fdc2e10cd57a09d0e6609fe87eed3

It’s like, sometimes it works well and it moves to the position where I want, but, sometimes, It moves the opposite way, if that makes sense?

I appreciate your help, you have no idea how much that means to me, it bothered me so much when I encountered this issue. :smiley:

I think that has something to do with it needing to be converted to the objectspace if I’m not mistaken. I’m not 100% sure since when I did it I just put ScalePart at 0,0,0

1 Like

Yeah, probably so. Anyway, thank you so much, I’ll mark it as the solution! :smile:

1 Like