Lockpicking position acting weird

The script and models are currently a placeholder but the model which is supposed to be the pick is not turning fully/properly towards the mouse and does strange rotations sometimes.
https://i.gyazo.com/3426daf8e5d68bb05658ce38a4afc8c0.mp4

local part = script.Parent.Frame.ViewportFrame.Model.Union
local Mouse = game.Players.LocalPlayer:GetMouse()

if Mouse then -- i know this should be here
	game:GetService("RunService").RenderStepped:Connect(function()
		
		local pos = Vector3.new(Mouse.Hit.Position.X, part.Position.Z, part.Position.Z)
		part.CFrame = CFrame.new(part.Position, pos) 
		
	end)
end

i have tried all x y and z in all mouse and part but none have gotten best/good results

This is because Mouse.Hit.Position is where the mouse lands in 3d space, so basically it’s touching actual parts in your Workspace. This means that, instead of touching the baseplate which is far away, it’s accidentally touching your lockpick model, meaning that Mouse.Hit.Position.Z would be closer to the camera than if it were to hit the Baseplate. That’s why it looks like it’s jumping forward.

As a quick sidenote, LocalPlayer’s Mouse is deprecated, and there are better alternatives out there. Try using UserInputService:GetMouseLocation(), which returns a Vector2 of the Mouse’s location on the camera Viewport. You can also use Camera:ViewportPointToRay() to get the mouse’s location in studs.

Here’s an example:

local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")

local part = script.Parent.Frame.ViewportFrame.Model.Union
RunService:BindToRenderStep("LOCKPICK_RENDERING", Enum.RenderPriority.Input.Value, function()
	local camera = workspace.CurrentCamera
	if camera then
		local mouseLocation = UserInputService:GetMouseLocation()
		local cameraRay = camera:ViewportPointToRay(mouseLocation.X, mouseLocation.Y)
		
		local partPosition = part.Position
		local mousePosition = Vector3.new(cameraRay.Direction.X, part.Position.Z, part.Position.Z)

		part.CFrame = CFrame.lookAt(partPosition, mousePosition)
	end
end)

Hope this helps.

That hasn’t done anything much lol https://gyazo.com/db6c9bb3057f5dd04aa6e9fee6c26f4f

I edited my code because I made a mistake. Try the new version.

Still, not much but it has done something. https://i.gyazo.com/69c211d4c11f65d6383b880a19ed07b4.mp4

I see. For the mousePosition variable, you could try doing Vector3.new(cameraRay.Direction.X, cameraRay.Direction.Y, part.Position.Z) which takes the camera ray’s Y direction into account, and leaves the part’s Z alone. This should solve everything.

This does work, however there are weird backwards areas. I will edit it a little bit and see where i gets me, Thanks!

1 Like

After a while of editing it i could not get it to rotate properly…

Closest thing i could get to was this however is just flies away
https://i.gyazo.com/82eff6d9817c76e676e8b4b5c9641886.mp4

1 Like