I’m using the iphone4GyroCam script to rotate a camera with my mobile device, it works fine but I would like to had to it the movement of the camera using a single joystick.
Basically, I’m trying to redo the FPS controller prefab using the gyro to look around instead of the right joystick.
This is the final Quaternion (after all the gyro input and device management script) that is used to rotate my camera:
the script being attached to the camera the transform.localRotation is the camera.
Obviously, if I attach that script to the FPS controller camera, the problem is that the forward movement of the FPS Char isn’t the forward of the camera.
The Fps controller has the rotation split in two, Yaw rotation is applied to the FPS controller and pitch rotation applied to the camera. Therefore the forward movement of the FPS char is always correct.
I’m really struggling to understand the Quaternions and their usage.
Is there a way of extracting the yaw of the final Quaternion applied to the camera and apply it to the FPS Controller?
Should I extract the rotation to eulerAngles and then turn it back to quaternions?
As this is for mobile, I need to find the less expansive way to do it…
If anyone of you has a good tutorial,website,book where the quaternions are explained it would be much appreciated, I always struggle with it…
Ok I’ve found a way to make it work without using the rotation. I’ve just use my camera alone and added to it the Character controller component and the first person Control script.
Everything seems to work fine.
But I’m still interested on some explanation about quaternions.
Quaternions are much like vectors really. Just like a vector can represent both a position or a direction, so can a Quaternion represent the rotation of an object in 3D space (transform.rotation) or a rotation direction (for example Quaternion.FromToRotation(Vector3 from, Vector3 to) or Quaternion.AngleAxis(float angle, Vector3 axis)).
So as with vectors you can “add” them to each other or “subtract” a rotation from another rotation. To add a rotation to another you can use the Quaternion.operator *. For example if you were looking forward and wanted to turn your head 45 degrees to the right, you would first create the head turning rotation by Quaternion headTurn = Quaternion.AngleAxis(45, Vector3.up), and then just add it to your head’s current rotation by head.rotation = headTurn * head.rotation; If you then wanted to turn your head back to where it was you would just subtract the headTurn by head.rotation = Quaternion.Inverse(headTurn) * head.rotation.
Check the Quaternion class in the script reference and take a look at its functions:
FromToRotation,
AngleAxis,
ToAngleAxis,
LookRotation
Angle
RotateTowards
and others are actually all you need to know about Quaternions. If you learn what those few functions do, you will become the master of your rotations.
So if you had a yawed and pitched rotation and you needed to extract the pitch you could do it by:
Vector3 forwardXZ = new Vector3(transform.forward.x, 0, transform.forward.z); // Clamp the thransform.forward direction to XZ plane
Quaternion pitch = Quaternion.FromToRotation(transform.forward, forwardXZ); // Get the rotation from current forward to the planar forward (pitch);
Yaw rotation in this case would be Quaternion.LookRotation(forwardXZ);
Thanks a lot for your explanation. I will really work on it, I’ve always struggled with them. I want to take bull by the horns and have it understood for good this time