How to make a part follow your mouse across a horizontal plane

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I would like a part to follow my mouse on the x and z axis

  2. What is the issue? Include screenshots / videos if possible!

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have looked through devforum but I could not find the topic that answered my question.

Hello guys! It has been a while but I have a question. How can I make a part follow my mouse only on the x and y axis? The point is that it should not be using mouse.Hit

I have been searching through the devforum but the only thing that I found is this.

local mousepart = workspace.MovePart
local rs = game:GetService("RunService")
local mouse = game.Players.LocalPlayer:GetMouse()
rs.RenderStepped:Connect(function()
	mousepart.Position = Vector3.new(mouse.hit.Position.X, 0, mouse.hit.Position.Z)
end)

Just for clearance: I am NOT new to coding, I have been coding for a while but this is something I have trouble with as I do not use mouse so much.

I thought I could use rays for that;

However I am not entirely sure how to do that.

Any help would be greatly appreciated!

3 Likes

Remote events.

If the mouse moves, you fire a remote event with the mouse.Hit.Position argument. Then with a script you check if the event is fired, if yes then put the parts position to the event argument.

I believe you mean x and z axes because y axis is the vertical axis in roblox. What you are trying to do is calculating the intersection point of the line going from the camera in the direction defined by the mouse.

You can get the mouse’s screen position using UserInputService:GetMouseLocation, convert this to a worldspace direction vector with Camera:ViewportPointToRay and the calculate a multiplier for the vector by dividing the total needed vertical movement by the vertical movement defined by the vector.

local UserInputService = game:GetService("UserInputService")

local planeY: number = 0 -- change this to whatever you want

local camera: Camera = workspace.CurrentCamera

local function getMousePositionOnPlane(): Vector3
	local mousePosOnScreen: Vector2 = UserInputService:GetMouseLocation()
	local mouseWorldSpaceRay: Ray = camera:ViewportPointToRay(mousePosOnScreen.X, mousePosOnScreen.Y)
	local origin: Vector3, direction: Vector3 = mouseWorldSpaceRay.Origin, mouseWorldSpaceRay.Direction
	local multiplier: number = (planeY - origin.Y) / direction.Y
	return origin + direction * multiplier
end
2 Likes

You may have misunderstood what my problem really is. I have already figured it all out how to change the parts position. What I want however is it follows an invisible horizontal plane.

Sorry everybody for the confusions that may arise. I made a seperate place for the video I made because I don’t want to spoil my upcoming game.

This may sound dumb but just change the x axis of the part ? That way it can only move in the X axis.
Like this :

local mousepart = workspace.MovePart
local rs = game:GetService("RunService")
local mouse = game.Players.LocalPlayer:GetMouse()
rs.RenderStepped:Connect(function()
	mousepart.Position = Vector3.new(mouse.hit.Position.X, 0, 0)
end)

Oops yes I keep messing it up when I try to say “x and z”

You’re trying to make the part only go one direction am I right ?

Im trying to make it go on the X and the Z axis, but as shown in the video if i hover my mouse over nothing the part dissapears

Thats really just the main problem I have…

Try changing this to

mousepart.Position = Vector3.new(mouse.Hit.Position.X, mousepart.Position.Y, mouse.Hit.Position.Z)

I think you would need to use mouse.Target/mouse.TargetFilter for what you’re trying to do, so that it stays on some sort of basepart.

Recheck this post he edit it to include what you need.
https://gyazo.com/151c27109b36684beb0ca67e98ed148c

Then you would need raycast, check if part is not hovering air.

local mousepart = workspace.MovePart
local rs = game:GetService("RunService")
local mouse = game.Players.LocalPlayer:GetMouse()

rs.RenderStepped:Connect(function()
	local direction = Vector3.new(0,-5,0) -- Direction
	local raycastResult = game.Workspace:Raycast(mousepart.Position, direction)

	if raycastResult then
		mousepart.Position = Vector3.new(mouse.hit.Position.X, 0, mouse.hit.Position.Z)
	end
end)

or you can also check if mouse is not on air.

local mousepart = workspace.MovePart
local rs = game:GetService("RunService")
local mouse = game.Players.LocalPlayer:GetMouse()

rs.RenderStepped:Connect(function()
	if mouse.Target ~= nil then
		mousepart.Position = Vector3.new(mouse.hit.Position.X, 0, mouse.hit.Position.Z)
	end
end)

He doesn’t seem to want it to stay on a BasePart, but instead at a constant y coordinate. This is a line-plane intersection problem.

@fancym0000, the code you posted will not give a position on the line going from the camera in the mouse direction unless the part surface happens to be at the correct height.

@davcio12344, I wrote code that should work in the general case. However, it will give an infinite/undefined result if the mouse direction happens to be parallel to the horizontal plane. What would be the desired result in this case? Should there a maximum distance or maximum horizontal distance from the camera for the point?

What does the OP want ? Stay in one line ? He just needs to update Z or X axis. Not the part the fall into the abyss ? Check mouse.Target or raycast before initializing part. I’m seriously confused

I mean I dont want people to be placing stuff from so far away so I guess maximum distance would be good

I meant X and Z axis, sorry for the confusion!

I think you should either use mouse.Target (best option i think) or raycast to check if part is hovering on air. If you use mouse.Target it would check the mouse BEFORE initializing the part, the first script about raycast will check if there is a part under the mousepart (so only when you’ll put the mousepart on the void, only after that he’ll detect it)

Ok well how do I move the part around then if its stuck? Gosh I didn’t think that this will be so difficult.