Getting a rotation based off of direction

Context: I’m working on a golf game and plan on changing gravity in different directions. However, I also want the camera to change orientation based on the gravity the ball is set to. The gravity is a unit direction where the default value is Vector3.new(0, -1, 0) and when the ball goes upside down gravity the direction is changed to Vector3.new(0, 1, 0). My system also uses a custom scripted camera so I can try any suggestion.

The goal is to get some sort of initial rotation I can apply before doing all the fancy camera stuff my system uses. For example, my camera is based off of cameraCFrame = subjectCFrame * cameraRotation * CFrame.new(0, 0, -zoom). I’m trying to change that to rotationFromGravity * cameraCFrame so that changing gravity doesn’t mess with the gameplay experience.

My main question is, how do I convert a unit direction into a rotation for my camera? Vector3.new(0, -1, 0) should return a rotation that is considered normal (like CFrame.Angles(0, 0, 0)), while Vector3.new(0, 1, 0) should return something like CFrame.Angles(math.rad(180), 0, 0) since the gravity would be upside down.

The wall walking controller EgoMoose created has a tutorial on how to rotate the camera simply based on a normal, which would be your unit gravity vector reversed in this case, at the bottom of the thread:

I hope that helps!

4 Likes

Thanks for showing me that thread, it worked perfectly! Thanks @EgoMoose for writing it too!

1 Like