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 letsfailsafe · Jul 22, 2013 at 12:21 PM · limit

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?

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 4 · 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
avatar image letsfailsafe · Jul 23, 2013 at 08:52 AM 0
Share

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);
         
     }
 }
avatar image letsfailsafe · Jul 23, 2013 at 08:52 AM 0
Share

According to the debug log fuel does reduce as I press the UpArrow key.

avatar image CHPedersen · Jul 23, 2013 at 10:24 AM 0
Share

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.

avatar image letsfailsafe · Jul 23, 2013 at 12:47 PM 0
Share

I have fixed the issue. The variable values were not high enough for it to make any change.

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

16 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 avatar image avatar image avatar image avatar image avatar image

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


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