Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
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
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · Mar 12, 2013 at 01:58 AM 0
Share

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.

avatar image thatnewguy · Mar 12, 2013 at 02:14 AM 0
Share

im sorry, but you lost me at flashlight.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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.
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges