Vector Maths with Unity Gizmos


It’s really nice to get a game finally published. Endless Elevator is now on the Google Play Store so I’ve been planning and experimenting and just plain playing around with Unity and Game Ideas before I start a new project. It’s kind of like a holiday.

I like maths and I also like visualising it with Unity. This little project is one of the simplest types of maths that is generally associated with game programming and that’s adding and subtracting vectors and getting the distance between two vectors.

These sorts of functions are endlessly useful. They answer questions like: How far away is my target? Is my Player going to fall off the platform? Can I jump between two platforms? And so on and so on.

What I like about this one is that it highlights two things. 1. The difference between a player and a target’s perspective (in this project I nominate the circle as the player and the cylinder as the target), and 2. calculating the magnitude or distance between them.

To calculate the distance (magnitude) between two points you take the square root of the square of the x and y values. Which sounds a little complicated but is really just making a square the size and length of your x, and adding that to a square the size of your y, and then getting the square root of the total size of that square.

This is how it looks in the Scene View:

How it works

The project is really easy to set up. All you need is an Empty Game Object to hold the script and two other objects. I used a Capsule and a Sphere. Drag the two objects into the public Transform components on the script.

The Project Hierarchy and Properties of the Script

Here is the reproduced script below:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor.UI;
    
public class GridGizmo : MonoBehaviour
{

    public Transform circle;
    public Transform capsule;
    public Vector3 differenceToCapsule;
    public Vector3 differenceToCircle;
    public float magnitude;

    void Update()
    {
        differenceToCapsule = capsule.position - circle.position;
        differenceToCircle = circle.position - capsule.position;
        /*
           Magnitude of a Vector (Length of the Vector)
           We use Pythagoras' theorem to calculate it:
           |a| = √( x2 + y2 )
        */
        magnitude = Mathf.Sqrt(Mathf.Pow(differenceToCapsule.x, 2) + Mathf.Pow(differenceToCapsule.y, 2));
    }


    public void OnDrawGizmos()
    {

        // Grid Number Labels
        for (int i = -10; i < 11; i++)
        {
        UnityEditor.Handles.Label(new Vector3(0f, i, 0f), "" + i);
        }

        for (int i = -20; i < 21; i++)
        {

            UnityEditor.Handles.Label(new Vector3(i, 0f, 0f), "" + i);
        }

        // Labels of Objects
        UnityEditor.Handles.Label(new Vector3(circle.position.x + 1, circle.position.y, circle.position.z), "POSI: " + circle.transform.position);
        UnityEditor.Handles.Label(new Vector3(circle.position.x + 1, circle.position.y - 0.5f, circle.position.z), "DIFF: " + differenceToCapsule);
        UnityEditor.Handles.Label(new Vector3(circle.position.x + 1, circle.position.y - 1f, circle.position.z), "Magnitude: " + magnitude);

        UnityEditor.Handles.Label(new Vector3(capsule.position.x + 1, capsule.position.y, capsule.transform.position.z), "POSI: " + capsule.transform.position);
        UnityEditor.Handles.Label(new Vector3(capsule.position.x + 1, capsule.position.y - 0.5f, capsule.transform.position.z), "DIFF: " + differenceToCircle);

        // The line
        Gizmos.color = Color.blue;
        Gizmos.DrawLine(circle.position, capsule.position);
    }
}

As I mentioned above I do like mathy things in Unity. Here are some of the other one’s I’ve done which are a bit more complicated. I find them really really useful to cement those ideas that I should know back to front but always forget exactly how they work.

http://localhost/2020/04/02/unity-circular-movement-with-triangles/
http://localhost/2019/06/04/unity-2d-curves-using-triangles/
http://localhost/2019/03/31/unity-how-to-rotate/
http://localhost/2018/10/31/why-normalize/


Leave a Reply

Your email address will not be published. Required fields are marked *