Right, so vectors.
A vector can indicate many things, and pretty common is rotation. For a game object, we tend to assume that the y axis is forward. So it’s z rotation is 0. This is confusing, right? The way it faces depends on a vertical thing.
With vectors, we can ignore this. A simple definition of a rotation vector is to draw a line from the center of a graph to a 3D location. So to find out what way our player is facing, instead of saying his z rotation is 0, we can say that his y axis is pointing along the vector [0,1,0] (his local axis is matching the global axis)
In blender’s API, we can get the way an object is facing using obj.getAxisVect([0,1,0]) for the y axis.
And so how about setting vectors? For that we use obj.alignAxisToVect(vector, 1, 0.5) where the 1 is the y axis and the 0.5 is to rotate only half-way this frame.
In this case, we may want to get the vector of the camera, but we only want the Z rotation of the camera. Unfortunately this isn’t as simple as grabbing the last component of the vector, because this is how much vertical it is facing. Instead we want only the x and y components, or we set the z component to zero.
So here’s some quick code:
camera_ori = camera.getAxisVect([0,0,1]) #because the z axis of a camera is forwards
desired_ori = camera_ori
desired_ori[2] = 0
player.alignAxisToVect(-desired_ori, 1, 0.5)
And if we want to align to a different axis (say the player is pressing A) we can simply set the axis differently:
player.alignAxisToVect(-desired_ori, 1, 0,5) #facing away from us. Remember that camera faces -z axis!!!
player.alignAxisToVect(desired_ori, 1, 0,5) #facing us
player.alignAxisToVect(desired_ori, 0, 0,5) #sideways
player.alignAxisToVect(-desired_ori, 0, 0,5) #other sideways
One other thing that may happen is he falls over sideways. If this is the case, you may have to align his Z axis to the world Z axis:
player.alignAxisToVect([0,0,1], 2, 1)
— edit, having read your post —
About whether this is the ‘right way’ there is no right way. Do whatever works, and what you understand.
As the writer of one web comic puts it: