Rotations


Rotations

Rotations in Unity can be quite confusing to work with, especially when changing an object's rotation dynamically with code. When looking into rotating objects in Unity one will eventually come to the concept of Quaternions. Quaternions are hard to read without performing some complex math equations, unlike Vectors which are straightforward. Luckily we can make cool and accurate rotations without having to actively read a Quaternion.

Unity stores rotations as a Quaternion to Gimbal Lock, a case where two axes of a three dimensional object become parallel "locking" the three axes. This is why Quaternions have a four number, X,Y,Z as a 3D Vector with W being a value that basically stores a rotation magnitude. As stated earlier Quaternions are not easily readable so Unity displays a Vector3 of Euler Angles it turns into a Quaternion.

Now that may seem like alot but you need to know that to explain why rotations are programmed in a particular manner. As I'm about to show, implementation of rotations can be really simple.

Camera

The security cameras in Snapback have two states: Searching and Alerted. While Searching the camera simply pans back and forth until it spots the player and goes into Alert mode which I talk about in a moment, but first let's talk about the camera pan.

 This is simply done by rotating the camera on the Y axis and rotating on the X axis to get the desired view angle. Now in our case the model has a series of joints we can rotate to achieve this with the “camera_head” being a child of the “camera_rotate_joint”, so we rotate the head to face the ground and only worry about rotating the joint along the Y axis. This can be achieved with a sine wave with Mathf.Sin and the function Quaternion.AngleAxis. Using Mathf.Sin we can use the time and a float to get an angle every frame that goes between the positive of our search angle to its negative.

 Quaternion.AngleAxis uses an angle and an axis to return a Quaternion. It is important to remember that the parent may have its own rotation which we need to account for. This is done by simply combining the two rotations which is done by multiplying them together.

In Alert mode the camera looks directly at the player until it loses sight of them. Now we could rotate the arm joint and camera head separately to achieve this effect … or we can cheat a little. Unity has a function called Quaternion.LookRotation, This function works by simply putting in a direction and it returns a Quaternion that is facing the direction inputted. We can apply this to the “camera_head” rotation directly as visually it will look correct. 

All that is left is to get the direction from the Camera_Head to the player which is as simple as subtracting the player's position from the Camera's position.

Turret

The turret only rotates when activated by the camera where it tracks the player until it is deactivated, however because the turret is slightly more complex in design we can't simply use Quaternion.LookRotation as it wouldn’t look right.

For this model we need to rotate the Turret arm along the Y axis and the Turret guns along the X Axis. We will technically be using the same method for both, however the guns will require an additional step which I will explain in a moment. Now we need the direction from the turret to the player. We will use this to find the angles to rotate towards. We'll use two functions Mathf.Atan2 and Mathf.Rad2Deg, the specifics of these two functions isn’t critical to this explanation but basically Atan2 gives as an arctangent which we convert the radian to a degree with Rad2Deg. Using these functions we get the angle we need to rotate toward to face the player. 

Now for the Turret_Arm we are finished but the Turret_Gun has a flaw. If the player stands at specific positions the turret starts to turn away or very slowly. These positions are when the direction toward the player, which is a vector 3, the absolute value of X is less than Z.This is a simple fix we just use the Z instead of the X when that condition is met.

This happens because while both are moving along one axis, the gun is affected by both the X and Y axis so the direction axis we need to use is changed. Using these angles we use our final function Quaternion.Euler which basically turns a Vector 3 into a Quaternion we can set as the rotation for each part.

Conclusion

Rotations can be confusing to work with, but aren’t impossible. With a little practice, some knowledge, and a few examples can make them more approachable.

I also want to explain why the “lookDirection” for the Camera and the “gunAngle” are negative. The models used are facing toward the negative of the Z axis at 0 Rotation, so these specific parts would face the opposite direction if we don’t invert these values first.

Get Snapback

Leave a comment

Log in with itch.io to leave a comment.