Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How to get the rotation from an existing CFrame?

Asked by 6 years ago

I'm doing something where I'd like the player to face the direction of the camera at all times. In this case, I cannot use any of the existing CameraTypes, as I am doing it in first person and the way that I've scripted the camera now prevents the player from turning when the camera is moved, like it normally does.

To fix this, I thought I would do Character:SetPrimaryPartCFrame(CFrame.new(HumanoidRootPart.Position, Camera.Rotation)) Although I don't know of any way to get the Camera's rotation, as there is no orientation property; only CFrame. So, my question is how do I get the rotation value from a CFrame?

1 answer

Log in to vote
0
Answered by
RayCurse 1518 Moderation Voter
6 years ago
Edited 6 years ago

In order to get the rotational part of a CFrame, all you have to do is subtract the position from the CFrame. Then, you can apply the rotation to the existing CFrame of the humanoid root part using the * operator.

local camera = workspace.CurrentCamera

--Rotation and position of the coordinate frame
local rotation = camera.CFrame - camera.CFrame.p
local position = CFrame.new(humanoidRootPart.Position)

--Apply the rotation to the position coordinate frame
local result = humanoidRootPart.CFrame * rotation

character:SetPrimaryPartCFrame(result)

It is important to note that when doing camera.CFrame - camera.CFrame.p, what we are really doing is translating or moving around the CFrame to the origin. For example, cf = cf + Vector3.new(0 , 1 , 0) will move cf up by one stud. When a CFrame is at the origin, it's position is essentially zero on all axes which means that applying this CFrame to another operand will leave its position untouched.

0
Thank you VERY much! This helped me out a lot cowsoncows 951 — 6y
Ad

Answer this question