• 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
1
Question by Okocha · Jun 25, 2013 at 05:23 AM · flyjetpack

How to make a jetpack with fuel?

Hi, I want to create a jetpack with fuel. I already got the script to fly with the jetpack.

 var speed : float = 6.0;
 var jumpSpeed : float = 8.0;
 var gravity : float = 20.0;
 var maxFuel : float = 100;
 var fuel : float = 10;
 
 private var moveDirection : Vector3 = Vector3.zero;
 function Update() {
     var controller : CharacterController = GetComponent(CharacterController);
     if (controller) {
         moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,
                                 Input.GetAxis("Vertical"));
         moveDirection = transform.TransformDirection(moveDirection);
         moveDirection *= speed;
         
         if (Input.GetButton ("Jump")) {
            moveDirection.y = jumpSpeed;
         }
     }
     moveDirection.y -= gravity * Time.deltaTime;
     
     controller.Move(moveDirection * Time.deltaTime);
 }


1) I want to create a variable with fuel. The fuel should reduce, when i press the "Jump" button. (Maximum Fuel should be 100, and every time the button Jump is pressed reduce 10 or like for every second the button is pressed the fuel reduce 10)

2) Also i want to fill the fuel for every second the player isGrounded. (+10 every second)

3) When the fuel count is 0, the jetpack shouldn't fly anymore. I want the var fuel dynamic, so that the current fuel should be always printed in the console.

I'm pretty new to unity and i hope someone could help me to fix the problem.

Comment
Add comment · Show 1
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 Okocha · Jun 25, 2013 at 11:54 PM -1
Share

Thank you both for your help and input. $$anonymous$$y new script works fine!

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by MorphingDragon · Jun 25, 2013 at 05:50 AM

1)

would rename fuel to dFuel just so the name makes more sense (Rate of change of fuel). You could make another variable, maybe currentFuel or whatever name you feel is right. Define the function Start() and inside set currentFuel to maxFuel. This will be called by Unity just before the scene starts.

To get the fuel to decrease at 10 units per second change the if statement to something like this:

  if (Input.GetButton ("Jump") && currentFuel > 0.0) {
         moveDirection.y = jumpSpeed;
         currentFuel = Mathf.Clamp(currentFuel - 
                   (dFuel * Time.deltaTime), 0.0, maxFuel);
     
 }

What this does is if we have fuel, move the character up and consume some fuel. The call to clamp just makes sure that currentFuel is never negative as this wouldn't make any sense and may cause bugs.

2)

he simple answer is that there is no best way for #2. You could look at using colliders and Unity will tell you when you've collided with another object. This has the draw back that you have to make sure that your collider information is always correct in the editor for it to work. I would probably use this method as it would be less bug prone in code and tracking editor information is relatively easy in Unity. You'd have to look more into this yourself as I'm not quite sure how to use the colliders.

Another option is to just keep a velocity or position of the last frame and compare it to the velocity of this frame. If the change in the two velocities is under some threshold then you consider it as being on the ground. The issue with this solution is that on the arc the change in velocity will meet this threshold in at least two places, at the top of the flight and on the ground. That's only assuming that the player consumes all their fuel before stopping as well.

3)

he code for #1 should also fix this scenario. To make a variable print to the console just use the print(currentFuel) function and place it in the update function.

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

Answer by slayer29179 · Jun 25, 2013 at 05:36 AM

This is simple my friend! I will try to explain it the best I can :)

The line :

 if (Input.GetButton ("Jump")) 
 {
    moveDirection.y = jumpSpeed;
 }

States that if the user presses a button known as Jump (Which is set inside Unity's options and is known as space) then move the player up using the jumpSpeed amount.

You can change this too: Not Tested

 if (Input.GetButton ("Jump") && fuel > 0) //If the button is pressed and the player has fuel
 {
    moveDirection.y = jumpSpeed;
    fuel -= 10 * Time.deltaTime; 
    print(fuel);
 ``//Take the fuel away every second by 10 when the jump     button is pressed
     }
 else if (fuel < maxFuel && isGrounded == true) //If the players fuel is less than the max fuel and the player is grounded
 {
     fuel += 10 * Time.deltaTime;
     print(fuel);
 //Increase the players fuel every second
 }

Hope this helps!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Related Questions

Constant Force applied to player without rigidbody on button press 1 Answer

Problems entering in Fly Mode 1 Answer

Nav Mesh Agent - Stuck on ground 1 Answer

Move Object a direction with one button and move the object back with the same button? 1 Answer

how can i put a aerial dashing script on my game? 0 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