- Home /
Question by
thatnewguy · Mar 12, 2013 at 01:55 AM ·
carnoob
basics of making a gas script (making cars get gas in a gas staition)
okay, so i have MINIMAL scripting skills, so i am looking for at least the basics of making a script that would control the amount of gas that a car has, and how much it gets when it goes to a gas station. any help for a noob?
Comment
You haven't defined much of your problem. I've only seen a few car/gas scripts, but if you Google this list for 'Flashlight,' you will find thousands of scripts. And the logic is similar or the same.
Answer by sparkzbarca · Mar 12, 2013 at 02:22 AM
simple script that tracks the amount of gas in a car
Public float Gas; //in liters because unity uses metric system
float KPL; //kilometers per liter, basically consumption rate
vector3 LastPosition; //this will just store our last position
vector3 CurrentPosition; //transform.position
float change; //how many kilometers we went between updates;
void start(){
LastPosition = transform.position;
CurrentPosition = transform.position;
change = 0f;
Gas = 10f; //the f just means 10 is a float number not an int
KPL = 10f;
}
//were going to do this in fixed update so that
//the change between frames isn't really small and
//subject to having really small changes being rounded
//down to zero.
FixedUpdate(){
CurrentPosition = transform.position;
change = CurrentPosition - LastPosition; //this is in meters. we multiply by 1000 to get kilometers
change = change * 1000;
//change now represents how much gas we've used up between frames
gas = gas - change /KPL;
//now we'll save our current position as our last
//so we can make use of it in the next update to
//calculate the change in distance and know how much gas
//was used
LastPostion = CurrentPosition;
if(gas <= 0)
{
//disable your movement script for you car, basically
//without gas you can't move
}
}
/*next we'll make a second script this will be attached to another object this will be the gas station. The gas station will have attached to it a collider MARKED AS A TRIGGER we will use the function OnTriggerEnter(Collider collider) to make it so when an object enters the bounds of the collider we check the object tag to see if it's a car and if so we increase its gas variable. Gas was earlier made public so we could modify it.
float HowMuchGasYouGetFromTheStation = 10;
void OnTriggerEnter(Collider collider){
if(collider.gameobject.tag == "car")
{
collider.gameobject.getcomponent<nameOfGasScript>().gas += HowMuchGasYouGetFromTheStation;
}
this script is loaded with some issues. you can keep getting gas there is no maximum and stuff but its a great solid starting point to modify off of.
Your answer
![](https://koobas.hobune.stream/wayback/20220613094238im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
simple car scripts 1 Answer
Car enter and exit script not working 1 Answer
Moveing my camera 1 Answer
car tutorial scripts question 1 Answer
Trigger Script Not Firing 4 Answers