Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I rotate a CFrame so that its UpVector faces a normalVector?

I'm making a hovercar, which needs to face away from whatever terrain it meets. To make it face away, I'm shooting a raycast straight down and usung the line CFrame.new(Part.Position, Part.Position+normal) which makes the CFrame's ForwardVector face the normalVector, not its UpwardVector, which means the car will be rotated to face downward.

Is there a way for me to either rotate the CFrame by 90 degrees, or to make the UpwardVector face the NormalVector?

1 comment

0

maybe you can use worldvector or worldspace or objectspace idk

1 answer

After an entire weekend of work, research and crying, i have finally found the answer. For reference here, "normal" is the normalVector of the raycast, Part is the part I'm raycasting from, turnFrame is the CFrame I'm trying to achieve.

local up = normal
local look = Vector3.new(normal.X,normal.Z, -normal.Y)
local right = up:Cross(look)
local turnFrame = CFrame.new(
    Part.Position.X, Part.Position.Y, Part.Position.Z,
    right.X, up.X, look.X,
    right.Y, up.Y, look.Y,
    right.Z, up.Z, look.Z
)

This allows for the vehicle to rotate across its local Y axis freely while following the normal, and for it to move around freely too.

Hope this helps any future readers of this thread!

This discussion is preserved as it was. New answers, comments, and votes are closed.