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

How to Rotate a Model Through the CFrame?

Asked by 5 years ago
Edited 5 years ago

I have a model here that I want to rotate and face wherever the player is facing. It's all good and all except that the model doesn't rotate completely how I want it. It faces the player but it rotates every coordinate. Here's the code

local model = script.Parent
local player = game.Players:WaitForChild("FakeName").CharacterAdded:Wait()

while wait(0.5) do
    model:SetPrimaryPartCFrame(CFrame.new(model.PrimaryPart.Position, player.HumanoidRootPart.Position) * CFrame.Angles(0, 0, math.rad(90)))

end

I have it so it faces the player and I want the primary part to go back to it's original basic rotation but whenever I multiply the angles and set it's Z coordinate to 90 and it's X coordinate to 0, it becomes a random number like (9.76, -176.83, 90) instead of keeping the X, Y, Z to 0 and 90.

1 answer

Log in to vote
1
Answered by
BuDeep 214 Moderation Voter
5 years ago
Edited 5 years ago
local model = script.Parent
local player = game.Players:WaitForChild("BuDeep").CharacterAdded:Wait()

while wait() do
    x, y, z, m11, m12, m13, m21, m22, m23, m31, m32, m33 = CFrame.new(model.PrimaryPart.Position, player.HumanoidRootPart.Position):GetComponents()
    model:SetPrimaryPartCFrame(CFrame.new(x, y, z, m11,m12,m13,m21,90,m23,m31,0,m33) * CFrame.Angles(0,0,math.pi/2))
end

The above should work, the x axis will be off by about 0.05 increments though (not noticeable at all). I gotta go to work but later on today I may try and fix that issue.

What I think happened is your cframe values were creating their own rotations for the part, and thus ignoring the multiplied cframe angle value. I manually got the components of the cframe and changed a couple of the upvector values to a 0 and a 90.

I'll try and post a better explanation tonight, let me know if you have issues!

1
Finally, thanks! I was looking for exactly this. Earlier I had the idea of GetComponents() but wasn't sure completely on how to use it well enough but you helped out greatly Retr0Thief 104 — 5y
Ad

Answer this question