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 moko · Jul 24, 2013 at 07:24 AM · movementcharactercontrollerjumpflyingjetpack

Jet-Pack Script

Hey guys,

I am trying to write a script for a jetpack. It should give the character controller the possibility to get up in the air if there's enough gas. furthermore the gas should increase when the controller is grounded and decrease when the controller is "flying".

That's my script: http://hastebin.com/lusafidiqa.coffee

It's really buggy and I don't know why. Sometimes at least some of the planned things work. I don't really get what I am doing wrong. Sry for not asking a specific question but it would be great if someone could check the script and tell me what's wrong with it. I am new to Unity and try to improve my scripting-skills.

Thanks

moko

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
Best Answer

Answer by robertbu · Jul 24, 2013 at 09:03 AM

You have a serious problem. The code as it is right now will hang Unity. The issue is the while loop on line 26 combined with you declaring gas an integer. So your while loop cycles, increment gas by some small fraction. Bug since gas is an integer, it never increased (stays zero), so the while loop never terminates. And you don't want a while loop anyway. There are a few more problematic things here as well. For example, you are using CharacterController.Move() and CharacterController.SimpleMove(). You should pick one or the other.

Here is the results of some quick hacking on your code. It could be written better, but it gives you a starting point. If you jump, you will go to the top of the jump height and stay there (flying) until you run out of gas.

 #pragma strict
 
 var groundSpeed : float = 5.0;
 var airSpeed : float = 10.0;
 var jumpSpeed : float = 1.0;
 var rotateSpeed : float = 3.0;
 var gas : float = 0.0;
 var gasStorageRate : float = 2.0;
 var gasUseRate : float = 4.0;
 var maxGas : float = 300.0;
 var gravity : float = 10.0;
 
 private var moveDirection : Vector3 = Vector3.zero;
 
 function Update() {
 
     var controller : CharacterController = GetComponent(CharacterController);
     
      // Move forward / sideward
     transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed, 0);
     var y = moveDirection.y;
     if (controller.isGrounded)
         moveDirection = transform.forward * groundSpeed * Input.GetAxis ("Vertical");
     else
         moveDirection = transform.forward * airSpeed * Input.GetAxis ("Vertical");
     
     moveDirection.y = y;
     moveDirection.y -= gravity * Time.deltaTime;
         
     if (controller.isGrounded) {
         gas += gasStorageRate * Time.deltaTime;
         if (gas > maxGas) gas = maxGas;
         if (Input.GetButton ("Jump")) {
             moveDirection.y = jumpSpeed;
         }
     } 
     else {
         gas -= gasUseRate * Time.deltaTime;
         if (gas < 0.0) gas = 0.0;
         if (gas > 0.0 && moveDirection.y < 0.0)
             moveDirection.y = 0.0;
     }
     
     // Move the controller
     controller.Move(moveDirection * Time.deltaTime);
 }

P.S. You will get more eyes looking at your question/code if you past a script into the question instead of using PasteBin. I'd only use PasteBin if your code is much longer than the script you provided.

Comment
Add comment · Show 2 · 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 moko · Jul 24, 2013 at 11:26 AM 0
Share

thank you so much! :) that helped me a lot.

avatar image Chase-Does · Apr 07, 2016 at 02:57 AM 0
Share

How do I control this code?

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

Character Controller - unknown rotation, flying 1 Answer

My Player can't jump - coding 1 Answer

Rigidbody Jumping for Character Controller 1 Answer

[C#] Jump on slopes 1 Answer

Adding force to the charactercontroller for walljump 1 Answer


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