[Unity Scripts] Gauge and Meter C# Scripts for your Unity Game

edited in Tutorials
Hi All,
I just finished the first pass on gauges for Orbital Dogfight in Unity.

I am sharing a few scripts for anyone who would like to use them / mod them.

If you have questions about how to get them working please post below. It does require you provide your own gauge/meter.

Here are my super basic gauges made in Rhino. A short video of them working is at the end.

image

The Random Jittering Gauge Script
This is a Single Needle Version of the next script "Fuel Gauge"

using UnityEngine;

public class Indicator_General : MonoBehaviour
{
    // Sets a "Needle" gameobject to the start rotation and jitters based on wanderspeed and amountToWander ... good for random meters like pressure, current, etc

    private Quaternion m_OriginalRotation;

    public float _AngleToStart = 100.0f;
    public float _AmountToWander = 0.5F;
    private float _rotationAngle;

    [Range(0, 1)]
    public float wanderspeed = 1.0f;

    private void Start()
    {
        m_OriginalRotation = transform.localRotation;
    }

    private void FixedUpdate()
    {
        _rotationAngle = UnityEngine.Random.Range(_AmountToWander, -_AmountToWander) + _AngleToStart;
        Quaternion newQuat = m_OriginalRotation * Quaternion.Euler(0, _rotationAngle, 0);
        transform.localRotation = Quaternion.Lerp(m_OriginalRotation, newQuat, Time.time * wanderspeed);
    }
}


The Fuel Gauge Script
This is a Dual Needle "Fuel Gauge"

using UnityEngine;

public class Instru_Fuel : MonoBehaviour {

    public GameObject needle_LOX;
    [Range(0, 360)]
    public float maxAngleLOX = 360.0f;
    [Range(0, 360)]
    public float startAngleLOX = 0f;

    public GameObject needle_Fuel;
    public float maxAngleF = 360.0f;
    [Range(0, 360)]
    public float startAngleF = 0f;

    private Quaternion m_OriginalRotationLOX;
    private Quaternion m_OriginalRotationFuel;

    public float _AmountToWander = 0.5F;
    private float _rotationAngleF;
    private float _rotationAngleLOX;

    [Range(0, 1)]
    public float wanderspeed = 1.0f;
    private float startFuel = 100f;

    private void Start()
    {
        m_OriginalRotationFuel = needle_Fuel.transform.localRotation;
        m_OriginalRotationLOX = needle_LOX.transform.localRotation;
    }


    private void FixedUpdate()
    {
        float fuelLevel = (startFuel - Time.deltaTime) / startFuel; // burning fuel over time as a cheap fix for a true fuel method
        float wander = Time.time * wanderspeed;
        _rotationAngleF = UnityEngine.Random.Range(_AmountToWander, -_AmountToWander) + (startAngleF * fuelLevel);
        Quaternion newQuat = m_OriginalRotationFuel * Quaternion.Euler(0, _rotationAngleF, 0);
        needle_Fuel.transform.localRotation = Quaternion.Lerp(m_OriginalRotationFuel, newQuat, wander);

        _rotationAngleLOX = UnityEngine.Random.Range(_AmountToWander, -_AmountToWander) + (startAngleLOX * fuelLevel);
        Quaternion newQuat2 = m_OriginalRotationLOX * Quaternion.Euler(0, -_rotationAngleLOX, 0);
        needle_LOX.transform.localRotation = Quaternion.Lerp(m_OriginalRotationLOX, newQuat2, wander);
    }
}


The Velocity Gauge Script

Note: Change the line:
_shipVel = shipController.VelocityMain;

to
_shipVel = _thisShip.GetComponent<Rigidbody>().velocity

then delete the MyFlightController junk that you dont need....

Can probably drop the IEnumerator "waitforinit" as well

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Instru_Velocity : MonoBehaviour {

    public GameObject _thisShip;

    public GameObject _Needle;

    [Tooltip("Angle at which the Needle with start/rest")]
    [Range(0, 360)]
    public float startAngle = 0f;

    [Tooltip("Angle range for the Needle")]
    [Range(0, 360)]
    public float maxAngle = 360.0f;

    public MyFlightController shipController;
    private bool rdy = false;
    private bool isBelowSeaLevel;
    private Quaternion m_NeedleOriginalRotation;

    private float _rot;
    private float _shipVel;

    private void Start()
    {
        m_NeedleOriginalRotation = _Needle.transform.localRotation;
        StartCoroutine("WaitforInit");
    }

    private IEnumerator WaitforInit()
    {
        yield return new WaitUntil(() => _thisShip.GetComponent<MyFlightController>() != null);
        shipController = _thisShip.GetComponent<MyFlightController>();
        rdy = true;
    }

    private void Update()
    {
        if (!rdy)
        {
            Debug.Log("NOT RDY");
            return;
        }

        _shipVel = shipController.VelocityMain;
        _rot = maxAngle * ( _shipVel * .001f); // meter maxes out at 1000 m/s.. get percentage of that total by ( _v / 1000.0f ) aka ( _v* .001f ) for optimization
        _rot = Mathf.Clamp(_rot, 0.0f, maxAngle); 
        _Needle.transform.localRotation = m_NeedleOriginalRotation * Quaternion.Euler(0, _rot, 0);
    }
}


Gauges in Video:

*Top Center Orb is the Flight Director with Vector overlays.

Top Left Down
Altimeter
Radar Altitude
Fuel

Top Right Down
Vertical Acceleration
Velocity
Heading





NOTE: If your needle(s) are rotating funny try placing them in an empty Gameobject and moving that to the needle's desired pivot point (You will need to move your needle back to the right spot after moving the empty parent to the pivot point.

Thanks for reading!

Best,

Moga
Thanked by 1konman
Sign In or Register to comment.