Moving the waist with the Player's Mouse

I’ve been trying to make this Player Mouse w/ waist movement work for hours now but can’t seem to figure it out. It would be really appreciated if someone would help me figure out what I’m doing wrong and what should I do to fix it. I’m sure this would be useful for others as well.

--//Variables
local UserInputService = game:GetService("UserInputService")
local Plr = game:GetService("Players").LocalPlayer
local xAngle = 0
local yAngle = 0

--//Functions
local function projectScreenPointToWorldGeometry(x, y)
	local ray = workspace.CurrentCamera:ScreenPointToRay(x, y)
	local _, pos = workspace:FindPartOnRay(Ray.new(ray.Origin, ray.Direction*5000))
	return pos
end

local function limitAngles(CF, X, Y)
	X = math.rad(X); Y = math.rad(Y)
	local x, y = CF:toEulerAnglesXYZ()
	return CFrame.new(CF.p) * CFrame.Angles(
		math.clamp(x, 0, X),
		math.clamp(y, -Y, Y),
		0
	)
end

game:GetService("RunService"):BindToRenderStep("CameraMove",Enum.RenderPriority.Camera.Value-1,function()

local rootPart = Plr.Character:FindFirstChild("HumanoidRootPart")
if Plr.Character and rootPart then
 local RotationOffset = (Plr.Character.PrimaryPart.CFrame- 
 Plr.Character.PrimaryPart.CFrame.p):inverse()
 local Pos = projectScreenPointToWorldGeometry(xAngle, yAngle) --// Pos as in the Target position of where I would want the Waist to face
 local RealPos = -(Plr.Character.PrimaryPart.CFrame.p - Pos).unit * 5000
 local TargetCFrame = RotationOffset * CFrame.new(Vector3.new(0, 0, 0), RealPos)
Waist.Transform = limitAngles(TargetCFrame, 0, 15)

end)

--// Mouse movement recorder
UserInputService.InputChanged:Connect(function(Input, gameProcessed)
	if Input.UserInputType == Enum.UserInputType.MouseMovement then
		xAngle = Input.Delta.X
		yAngle = Input.Delta.Y
   end
end)
5 Likes

That’s what I am using in this code. Are there any other suggestions? I’m looking for an answer that would basically point on my mistakes that I’ve done in the following code.

1 Like

Line 2 and 3 of UserInputService.InputChanged:

if Input.UserInputType == Enum.UserInputType.MouseMovement then
	xAngle = Input.Delta.X -- here,
	yAngle = Input.Delta.Y -- and here
end

Camera:ScreenPointToRay uses the position of the mouse, not the delta. Just change the xAngle and yAngle variables to record Input.Position instead, and you should be able to accurately cast the position from the ray.

3 Likes

Hey, golden!
Thank you for your answer. The code still doesn’t let me change the waist position, so I’m guessing the problem is laying somewhere in the RenderStep loop. Do you have any suggestions?

1 Like

Figured out another way of doing this! I’m dropping the code here if there is anyone in need of it:
P.S Place the Folder under StarterCharacterScripts.

WaistRotationScript.rbxm (2.1 KB)

literally 2 lines of code ;

	local space = root.CFrame:toObjectSpace(Mouse.Hit).lookVector.unit
	waist.C0 = originWaistC0*CFrame.Angles(math.asin(space.Y),-math.asin(space.X),0)

is there any way to make this toggleable? for example when holding a tool its active and when unequipped it returns to normal