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
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
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
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”:
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.