How can I get the players torso to face the mouse hit LookVector?

Hello all, I’ve been scripting in the Roblox engine for awhile now, but there’s one area that I have left thoroughly unexplored. CFrame math. Which, I have found by far the most confusing, yet crucial aspect of game development.

The issue in question here relates to continuously rotating the “Waist” Motor found within the players UpperTorso to face the Mouse.Hit LookVector to achieve the effect of the players torso always facing towards the mouse, disregarding player rotation or the cameras positioning. (With some obvious prevention of the player being able to rotate a full 180 degrees to face right behind them)

With some trial and error. This is a very sub-par approach I managed to create:


local NewMouseCFrame = Mouse.hit:ToObjectSpace(Torso.CFrame)

local NewTestCFrame=CFrame.new(WaistMotor.Transform.p,NewMouseCFrame.p)
	
local Look = NewTestCFrame.LookVector
		
WaistMotor.C0 = CFrame.Angles(0,Look.X,0)

fc7c0f5b6c52acf2eed2fbce8c0ab8ef


This is the result. That although looks passable, Has problems such as snapping to positions and the radians switching sides when the player is walking towards the camera. (Player faces opposite to the mouse rather then towards it)

Thank you for taking your time to read this post. I hope that you can help me with this issue. :smile:

1 Like

So from what I understood you want smoother movement, for that you’ll use lerp so with your current code you’d do something like this :

local Mouse = game.Players.LocalPlayer:GetMouse()

local Torso = game.Players.LocalPlayer.Character:WaitForChild("UpperTorso")
local WaistMotor = Torso.Waist

local RunService = game:GetService("RunService")
local Heartbeat = RunService.Heartbeat


while true do
	local NewMouseCFrame = Mouse.hit:ToObjectSpace(Torso.CFrame)
	
	local NewTestCFrame=CFrame.new(WaistMotor.Transform.p,NewMouseCFrame.p)
		
	local Look = NewTestCFrame.LookVector
	
	local percentage = (Look.X/(WaistMotor.C0.X - Look.X))
	warn(percentage)
	percentage = math.clamp(percentage,.1,1)
				
	WaistMotor.C0 = WaistMotor.C0:Lerp(CFrame.Angles(0,Look.X,0),percentage)
	Heartbeat:Wait()
end
6 Likes

Yes, I thought about using a clamp, just wasn’t sure how to implement it in conjunction with radians. Didn’t think about utilizing lerp for this. Thank you.

1 Like