How to Calculate Angles For GameDev – A Unity Math Tutorial

I'll guide you through calculating angles in Unity, using a first-person shooter (FPS) game as an example to illustrate the concepts.

I’ll guide you through calculating angles in Unity, using a first-person shooter (FPS) game as an example to illustrate the concepts.

Understanding Trigonometric Functions

In FPS games, precise aiming is crucial. To achieve this, we need to understand trigonometric functions like sine, cosine, and tangent. These functions help us calculate angles and sides of triangles, which is essential for aiming mechanics.

The SOH CAH TOA Acronym

A handy acronym to remember these functions is SOH CAH TOA:

  • SOH: Sine = Opposite / Hypotenuse
  • CAH: Cosine = Adjacent / Hypotenuse
  • TOA: Tangent = Opposite / Adjacent

In our FPS context, we’ll focus on tangent, as it’s most relevant for calculating aim angles.

Calculating the Angle

To calculate the angle between our weapon and the target, we use the inverse tangent (arctan) function. In Unity, we use Mathf.Atan2() which handles both positive and negative values.

Here’s how we calculate the angle:

float angle = Mathf.Atan2(direction.y, direction.x);

This gives us the angle in radians. To convert to degrees:

float angleDegrees = angle * Mathf.Rad2Deg;

Implementing in Unity

Let’s implement this in a Unity script for our FPS weapon:

public class WeaponAim : MonoBehaviour
{
    public Transform target;

    void Update()
    {
        Vector3 direction = target.position - transform.position;
        float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;

        // Adjust for Unity's coordinate system
        angle -= 90f;

        transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
    }
}

This script will make our weapon rotate to face the target.

Aiming at Mouse Position

For aiming at the mouse position, which is common in FPS games, we need to convert screen coordinates to world coordinates:

Vector3 mousePosition = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10f));
Vector3 direction = mousePosition - transform.position;
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg - 90f;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);

This code will make our weapon aim towards the mouse cursor.

Understanding Unity’s Coordinate System

It’s important to note that Unity’s coordinate system is different from the standard mathematical coordinate system. In Unity, 0 degrees points upwards, and rotation is clockwise. That’s why we subtract 90 degrees from our calculated angle.

Quaternions vs Euler Angles

Unity uses Quaternions for rotations internally, but we can work with Euler angles for simplicity. Here’s how to directly set Euler angles:

transform.eulerAngles = Vector3.forward * angle;

This approach is often easier to understand and use, especially for 2D rotations.

Conclusion

Calculating angles in Unity is a fundamental skill for game developers, especially when creating aiming mechanics for FPS games. By understanding trigonometric functions and Unity’s coordinate system, you can create precise and responsive aiming systems. Remember to always consider the context of your game and adjust calculations accordingly.