• 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 dizzymediainc · May 19, 2015 at 09:02 AM · jetpack

Jetpack script adjustments

Hello everyone, I have a jetpack script that I made and am having a few issues, for starters my gameplay is a 3rd person platformer that has an open world feature. In one of my worlds you are on the moon and can use the jetpack to jet around, only I don't want the player to be able to jet into the air forever so I would like to add a "gas" feature with a visible bar in the lower middle of the screen.

(When player holds F button jetpack is activated LIFT UP, when player is not grounded jet animations are activated, when player is grounded jet animations are de-activated)

This is where my issue is, does anyone have a good way of doing this with my script? Thanks!

Script:

 using UnityEngine;
 using System.Collections;
 
 public class JetPack : MonoBehaviour {
 
    public float JetPackForce;
    public float startTime = 3.5f;
    public GameObject player;
    public GameObject Rocket1;
    public GameObject Rocket2;
    Animator anim;
    
    void Start(){
        
         anim = GetComponent<Animator>();
        
       }
    void FixedUpdate () {
       if (Input.GetKey (KeyCode.F)) {
          Rocket1.SetActive(true);
          Rocket2.SetActive(true);
          startTime -= Time.deltaTime;
          player.rigidbody.AddForce(new Vector3(0.0f, 1.0f, 0.0f) * JetPackForce);
 
             }
             
       if (startTime <= 0.0f) {
          player.rigidbody.velocity = Vector3.down * Time.smoothDeltaTime;
          startTime = 3.5f;
             }
             
      if(anim.GetBool("Grounded")) {
           Rocket1.SetActive(false);
          Rocket2.SetActive(false);
 
      }
    }
 }
 
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
0

Answer by dizzymediainc · May 21, 2015 at 06:36 AM

I ended up figuring it out, here's the adjusted code: (minus the visible bar which i will handle in NGUI)

 using UnityEngine;
 using System.Collections;
 
 public class JetPack : MonoBehaviour {
 
    public float JetPackForce;
    public float startTime = 3.5f;
    public float fuel = 10;
    public float maxFuel = 100;
    public bool isGrounded;
    public GameObject player;
    public GameObject Rocket1;
    public GameObject Rocket2;
    Animator anim;
    
    void Start(){
        
         anim = GetComponent<Animator>();
 
   }
    void FixedUpdate () {
       if (Input.GetKey (KeyCode.F) && fuel > 0) {
          Rocket1.SetActive(true);
          Rocket2.SetActive(true);
          startTime -= Time.deltaTime;
          player.rigidbody.AddForce(new Vector3(0.0f, 1.0f, 0.0f) * JetPackForce);
          fuel -= 10 * Time.deltaTime; 
 
             }
             
       if (startTime <= 0.0f) {
          player.rigidbody.velocity = Vector3.down * Time.smoothDeltaTime;
          startTime = 3.5f;
             }
             
      if(anim.GetBool("Grounded")) {
           Rocket1.SetActive(false);
          Rocket2.SetActive(false);
          isGrounded = true;
      }
 
          else{ 
          isGrounded = false;
     }
      
      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
  
      }
      
    }
 }
 
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

2 People are following this question.

avatar image avatar image

Related Questions

Making a Jetpack 3 Answers

camera doesnt move after switching animation 0 Answers

JetPackParticleController not working in the 3D Platformet Tutorial 0 Answers

Help with jetpack script 1 Answer

Where do I start in unity, multiplayer spe 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