Help with CFrame rotation!

Hey there! So basically I want the players character Torso to move to where the player is facing. With this I would like it to be able to do a clamp thing. What I mean by this is if you turn so much to the left/right then your torso moves. If you can help me with this it would be great. You dont need to know anything about VR since its just CFrame.

WHAT IT LOOKS LIKE: VIDEO
[In RenderStepped]

vrCharacter["Torso"].CFrame = CFrame.new(vrCharacter["Head"].Position - Vector3.new(0, 1, 0))

This doesn’t work for some reason it makes my torso just disappear.

This script does this:

The texture is the front of the white part

RunService.RenderStepped:Connect(function()
	WS.WhitePart.CFrame = WS.BlackPart.CFrame + WS.BlackPart.CFrame.LookVector * -4
end)

Its the players torso though, I dont want it rotating around them but just facing the direction they are looking.

Like this?
I had not anchored it, therefore it jumps around

RunService.RenderStepped:Connect(function()
	WS.WhitePart.CFrame = WS.BlackPart.CFrame * CFrame.new(0, 2, 0) + WS.BlackPart.CFrame.LookVector * 0
end)

Something like that yes, but it cant be rotated on the X and Z axis only Y.

So this doesn’t work?

RunService.RenderStepped:Connect(function()
	local head = WS.BlackPart
	local torso = WS.WhitePart
	torso.CFrame = head.CFrame * CFrame.new(0, -2, 0) + head.CFrame.LookVector * 0
end)

This is working but like I said I dont want the torso to be rotating on the X and Y since now if I look down I cant see my torso.

Interesting concept.
The biggest issue here is that humanoids do not like their torso being tilted to much up and down and it causes very strange behaviour.
So you need to be carful to apply only yaw, while ignoring (or heavily reducing) roll and pitch.

I just realized while writing this, that you are probably not using a humanoid in the first place.

Also you mentioned you would like the torso to do “clamp thing” aka hysteresis.

Let’s see.
Do you already have camera render? You can get it this way. I suggest to always use this instead of head rotation. Use the source material whenever possible.

local camRender =  workspace.Camera:GetRenderCFrame()

I am assuming you are not using humanoids. This solution will only work for non-humanoid objects.
We are only interested in yaw rotation, and not in roll or pitch.

For stiff movement you will have something like this. This script will probably mess up your movement script as it will overwrite torso position. You will need to combine it with your current movement script:

vrCharacter["Torso"].CFrame = CFrame.new(vrCharacter["Torso"].Position,Vector3.new(camRender.LookVector.X,0,camRender.LookVector.Z)

For “clamp thing” you will probably need to do some extra checks. I would probably use Orientation Lets assume maximum leeway is about 45 degree:

local x,y,z = camRender:ToOrienation
local diff = math.abs(vrCharacter["Torso"].Orientation.Y - math.deg(y)) 
if diff > 45 and diff < 315 then
 CFrame.new(vrCharacter["Torso"].Position,Vector3.new(camRender.LookVector.X,0,camRender.LookVector.Z)
end

This is what happens when I use this VIDEO

		local camRender = camera:GetRenderCFrame()
		local x,y,z = camRender:ToOrientation()
		local diff = math.abs(vrCharacter["Torso"].Orientation.Y - math.deg(y)) 
		if (diff > 45 and diff < 315) then
			vrCharacter["Torso"].CFrame = CFrame.new(vrCharacter["Torso"].Position, Vector3.new(camRender.LookVector.X, 0, camRender.LookVector.Z))
		end

Yeah sorry my bad. Not my best day.
Here is the fixed version (I have tested it this time):

--prep
local torso = vrCharacter["Torso"]

--inside RenderStepped
	local camRender =  cam:GetRenderCFrame()

	local x,y,z = camRender:ToOrientation()
	local diff = torso.Orientation.Y - math.deg(y) 
	if diff > 45 and diff < 315 then 
		torso.CFrame = CFrame.new(torso.Position) * CFrame.Angles(0,y,0)
	elseif diff < -45 and diff > -315 then
		torso.CFrame = CFrame.new(torso.Position) * CFrame.Angles(0,y,0)
	end
1 Like

Thank you this works! Problem is the “hysteresis” doesn’t work correctly because of my snap turn method but that’s fine lol.