Trying to make part follow Mouse Y Axis

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 to find out how to make a part’s Y axis the same as the level of my mouse’s Y axis but in world space.

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

I can’t figure out how to find the 3d Y Point that is equivalent to my 2d Y Mouse location.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I’ve tried to use Mouse.Hit.p but since that just returns the intersection between the mouse’s internal ray with an object in the space it produces inaccurate results.

Here’s a video of how the inaccurate results looked:

Here’s the code with Mouse.Hit.p

local part = workspace.MovePart
local userInputService = game:GetService("UserInputService")
local Mouse = game.Players.LocalPlayer:GetMouse()

userInputService.InputChanged:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseMovement then
		local hitp = Mouse.Hit.p
		part.Position = Vector3.new(part.Position.X, hitp.Y, part.Position.Z)
	end
end)
1 Like

Try using this, it worked fine for me. It’s not much of a difference, but there’s no issue for me. (I created a setup similar to yours)

local part = workspace.MovePart
local userInputService = game:GetService("UserInputService")
local Mouse = game.Players.LocalPlayer:GetMouse()

userInputService.InputChanged:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseMovement and Mouse.Target ~= nil then
		local hitp = Mouse.Hit.Position.Y
		part.Position = Vector3.new(part.Position.X, hitp, part.Position.Z)
	end
end)

This sort of works but only when there’s walls behind it, the ideal goal is to have the height of the part be the same as the height of the mouse at all times.

you have to use ray cast not mouse hit position

Could you tell me how I could do it? I’m not very experienced with raycasting, maybe some code? Thanks :smile:

local rayOrigin = mouse.hit.p
local rayDirection = currentcamera.position.Y*1000

local raycastResult = workspace:Raycast(rayOrigin, rayDirection)

Pretty sure this wouldn’t work, why would the rayOrigin be mouse.hit.p…?

sorry it ment to be the camera and rayDirection be the mouse + range

local rayOrigin = ----current camera position
local rayDirection = ---mouse hit position + vector3.new(range,range,range)
local raycastResult = workspace:Raycast(rayOrigin, rayDirection)

This is a bump, still looking for a proper help. :smile: