Mouse.hit.p.y not working properly

Hi everyone. So recently I have been trying to create a dragging system and began implementing fixed axis. The X and Z work perfectly but the y axis is giving me a tough time.

		if Axis == "All" then
			Target.Position = Mouse.Hit.Position + Vector3.new(0,Target.Size.Y/2,0)
		elseif Axis == "X" then
			Target.Position = Vector3.new(Mouse.Hit.p.X, Target.Position.Y, Target.Position.Z)
		elseif Axis == "Y" then
			Target.Position = Vector3.new(Target.Position.X, Mouse.Hit.p.Y, Target.Position.Z)
			print(Mouse.Hit.p.Y)
		elseif Axis == "Z" then
			Target.Position = Vector3.new(Target.Position.X, Target.Position.Y, Mouse.Hit.p.Z)
			
		end
		

1 Like

If you need me to provide more code, please let me know

I think you’re better off getting the mouse delta and then offsetting the part’s Y position by that. I think it’s because the mouse’s target position is so far away that it’s creating some inconsistencies.

local mouseDelta = Vector2.new()

local userInputService = game:GetService("UserInputService")

game:GetService("RunService").Heartbeat:Connect(function() 
mouseDelta = userInputService:GetMouseLocation() - mouseDelta
end)

--//use Y position of mouseDelta to offset the part's position
1 Like

ok but the part would be way too high if I ended up using the y position. how would i make it a smaller number?

You can scale it down by multiplying it so you get something more usable.

it seems to be flickering when I move it.

Instead of using Heartbeat use RenderStepped:
If you have locked mouse behavior use :GetMouseDelta() instead of :GetMouseLocation() - mouseDelta

local mouseDelta = Vector2.new()

local userInputService = game:GetService("UserInputService")

game:GetService("RunService").RenderStepped:Connect(function() 
    mouseDelta = userInputService:GetMouseDelta()
end)

-- use Y position of mouseDelta to offset the part's position

If you have not locked mouse behavior you should instead use :GetMouseLocation() - prevMouseLocation, :GetMouseLocation() - mouseDelta will give undesired results:

local UserInputService = game:GetService("UserInputService")

local mouseDelta = Vector2.new()
local prevMouseLocation = UserInputService:GetMouseLocation()

game:GetService("RunService").RenderStepped:Connect(function() 
    local newMouseLocation = UserInputService:GetMouseLocation()
    mouseDelta = newMouseLocation - prevMouseLocation
    prevMouseLocation = newMouseLocation 
end)

-- use Y position of mouseDelta to offset the part's position

It is not smooth. It looks very choppy and sometimes delta returns a negative number
robloxapp-20220319-2141181.wmv (302.3 KB)

This is the intented behavior, what you need to do now is multiply the Y position by some negative scalar, which is what is meant by “use [the] Y position of mouseDelta to offset the part’s position”:

Try multiplying it by something like -0.075.

what negative number would be required to fix this issue?

That’s up for you to determine, use trial and error until you get a satisfactory result. You might also consider using UserInputService.MouseDeltaSensitivity in your calculation.