- Home /
Having a fuel limit
Hi, please walk me through how to have a fuel limit to a object (spacecraft)
I'm having trouble coding it to reduce upon the press of a key... (I believe you have to create a float value and work on from there... right?)
Also what is the best way to have a fuel bar or gauge? GUI text?
Answer by CHPedersen · Jul 22, 2013 at 12:44 PM
If you want your fuel value to be a decimal number, then you can use a float. If you want it to be an integer value, use an int. But you're right you need a variable to hold the number. :) You could have something like this:
float fuelLevel = 100; // Start fuel
float moveSpeed = 0.1f; // Movement speed
private void Update()
{
// Move as long as Up arrow is pressed and the fuel is greater than 0
if (Input.GetKey(KeyCode.UpArrow) && fuelLevel > 0)
{
// Move forward
transform.Translate(transform.forward * moveSpeed * Time.deltaTime);
// Will decrease by 1 per rendered frame where you move! Might be too fast?
fuelLevel--;
// Will crease by 0.1 each frame instead. Maybe this is better?
fuelLevel -= 0.1f;
// Make fuel consumption depend on the frame rate just like the movement speed. Maybe that's even more realistic?
fuelLevel -= 0.1f * Time.deltaTime;
}
}
This would move the ship forward when the Up arrow is held down, and there is still fuel in the tank. Choose one of the schemes for decreasing the fuel level, or come up with your own, even better one! :)
For the gauge, I would start out with a bar, since it's easier to set up using two differently textured GUI.Box. But that's a topic for another question.
Hi, my spacecraft will no longer respond to my control systems...
$$anonymous$$y code with added fuel limiting system:
using UnityEngine;
using System.Collections;
public class L$$anonymous$$FuelScript : $$anonymous$$onoBehaviour
{
public float EnterySpeed = 10;
public float $$anonymous$$ainThrust = 20F;
public float RollThrust = 1F;
public float FuelLevel = 100;
void Start ()
{
rigidbody.velocity = new Vector3 (EnterySpeed,0,0);
}
void Update ()
{
//Landing $$anonymous$$odule applied force control
float thrust = Input.GetAxis("Vertical") * $$anonymous$$ainThrust;
float rot = Input.GetAxis("Horizontal") * RollThrust;
rigidbody.AddForce(transform.up * thrust);
rigidbody.AddTorque(0,0,-rot);
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.UpArrow) && FuelLevel > 0)
{
FuelLevel -= 0.1F * Time.deltaTime;
Debug.Log (FuelLevel);
}
}
}
$$anonymous$$y previous code (space craft responded to my controls):
using UnityEngine;
using System.Collections;
public class L$$anonymous$$PhysicsScript : $$anonymous$$onoBehaviour
{
public float EnterySpeed = 10;
public float $$anonymous$$ainThrust = 20F;
public float RollThrust = 1F;
void Start ()
{
rigidbody.velocity = new Vector3 (EnterySpeed,0,0);
}
void Update ()
{
//Lunar $$anonymous$$odule applied force control
float thrust = Input.GetAxis("Vertical") * $$anonymous$$ainThrust;
float rot = Input.GetAxis("Horizontal") * RollThrust;
rigidbody.AddForce(transform.up * thrust);
rigidbody.AddTorque(0,0,-rot);
Debug.Log (thrust);
}
}
According to the debug log fuel does reduce as I press the UpArrow key.
It's not immediately clear why your input controls stop working. It seems to me you've mixed up the two types of listening for input; one, using GetAxis, and then the Get$$anonymous$$ey one I used. Yours is correct (since it allows the user to configure which key corresponds to which axis). I only used Get$$anonymous$$ey for the sake of providing an example. If your space ship is designed to move when the result of GetAxis is non-zero, then fuel should decrease in that event, too.
If the spaceship stopped moving, put the values returned by GetAxis and stored in rot and thrust into a couple of calls to Debug.Log to see what is actually getting calculated.
I have fixed the issue. The variable values were not high enough for it to make any change.
Your answer
Follow this Question
Related Questions
Controlling camera rotation limits. 1 Answer
What is asset Server client license? 2 Answers
limit car collisions 1 Answer
Camera Limit Rotations Problem 1 Answer
Maximum scene size? 2 Answers