Hi, I have made a fuel limit to my spacecraft and I am having trouble pulling that fuel value and displaying it using GUI.
How would I be able to display the left over fuels?
Here is my full script for it:
using UnityEngine;
using System.Collections;
public class LMScript : MonoBehaviour
{
public float EnterySpeed = 10;
public float MainThrust = 9000F;
public float RollThrust = 1000F;
public float FuelLevel = 100;
public float FuelReductionLevel = 0.8F;
void Start ()
{
rigidbody.velocity = new Vector3 (EnterySpeed,0,0);
}
void Update ()
{
//Fuel script
if(Input.GetKey(KeyCode.UpArrow) && FuelLevel > 0)
{
FuelLevel -= FuelReductionLevel * Time.deltaTime;
Debug.Log (FuelLevel);
}
//Landing Module applied force control
float thrust = Input.GetAxis("Vertical") * MainThrust;
float rot = Input.GetAxis("Horizontal") * RollThrust;
rigidbody.AddForce(transform.up * thrust);
rigidbody.AddTorque(0,0,-rot);
}
}
And here is my measurements display script:
using UnityEngine;
using System.Collections;
public class UnitDisplayScript : MonoBehaviour
{
public Transform FuelReading;
private float speed;
void FixedUpdate()
{
speed = Mathf.Round(rigidbody.velocity.magnitude);
}
void OnGUI()
{
GUI.Box(new Rect(10,10,200,180), "Measurements");
//Resultant velocity display
GUI.Label(new Rect(20,40,120,20), "Velocity: " + speed + " m/s");
//Altitude display
GUI.Label(new Rect(20,70,120,20), "Altitude: " + Mathf.Round(transform.position.y) + " meters");
//Location display
GUI.Label(new Rect (20,100,160,20), "Position: " + ((int)transform.position.x).ToString() + "." + ((int)transform.position.y).ToString() + ".0");
//Target location display
GUI.Label(new Rect(20,130,120,30), "Target: 50.20.0");
//Display the fuel values in %
GUI.Label(new Rect(20,160,120,30), "Fuel: " + FuelReading.GetComponent("LMScript").FuelLevel + "%");
}
}