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 LeathalNinja1986 · Oct 17, 2011 at 03:07 AM · jumppower up

2D Charge Jump Complications

I have been having trouble getting a charge jump to work. I am a beginner in java script and any help would be helpful. The script doesn't cause any errors but does not cause "jump" when the space key is released.

 #pragma strict
 public var jumpUpVel : float = 10f;
 public var jumpForwardVel : float = 10f;
 var Gravity : float = 10f;
 var chargeLevel : float = 0; 
 
 //Don't change this in the inspector.
 var chargeSpeed : float = 1; 
 
 //Default, the charge will go up 1 per second
 var isCharging = false;
 
 function Update () {
 
      if (Input.GetKey(KeyCode.Space)) { //Did the user click?
           if(!isCharging) { //Some what unnecessary due to the way the Input is
           // setup
                isCharging = true;
                CalculateCharge();
           }
      }
 }
 
 function CalculateCharge () {
      while(Input.GetKey(KeyCode.Space)) { //Add to the charge as long as the
      // user is holding the button
           chargeLevel += Time.deltaTime * chargeSpeed;
     }
     if (Input.GetKeyUp(KeyCode.Space)){
        this.gameObject.rigidbody.AddForce(jumpUpVel, jumpForwardVel, 0);
         //transform.position.x++; 
     }
      // launch player
      // Reset the vars.
      chargeLevel = 0.0;
      isCharging = 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

2 Replies

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

Answer by syclamoth · Oct 17, 2011 at 03:20 AM

One problem- Input.GetKey(someKey) will never be true on the same frame that Input.GetKeyUp(someKey) is true! This means that the bit inside of calculateCharge that does the launching never actually gets called. The other problem is that CalculateCharge only ever gets called once- since you deactivate the bit that calls it immediately before it gets called the first time.

You're mostly right- you just need to rearrange your code a bit.

Let me show you!

 #pragma strict
 
 public var jumpUpVel : float = 10f;
 public var jumpForwardVel : float = 10f;
 var Gravity : float = 10f;
 var chargeLevel : float = 0; 
 //Don't change this in the inspector.
 var chargeSpeed : float = 1; 
 //Default, the charge will go up 1 per second

 // Get rid of this, it's not doing you any good.
 //var isCharging = false;
 
 function Update () {
 
      if (Input.GetKey(KeyCode.Space)) { //Is the user Clicking?
           chargeLevel += Time.deltaTime * chargeSpeed; // Increase charge
      }
      if (Input.GetKeyUp(KeyCode.Space)) { // Did the player just stop clicking?
           // do the actual jumping here-
           // this takes the actual charge level into account-
           this.gameObject.rigidbody.AddForce(Vector3(jumpUpVel, jumpForwardVel, 0) * chargeLevel, ForceMode.Impulse);
           // now reset the charge level!
           chargeLevel = 0;
      }
 }
 

A word of warning- using a while loop with variables which are only reset every frame is a good way to crash your game- so this bit of code here-

 while(Input.GetKey(KeyCode.Space)) {
     //some stuff which will happen forever
 }

will put your game into an infinite loop, and freeze it, because Input.GetKey(KeyCode.Space) will only ever evaluate false in the next frame- which your game will never reach, because it is waiting for GetKey(KeyCode.Space) to evaluate false! Which is to say, it is waiting for something to happen, which is in turn waiting for it. So, your program will freeze!

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
0

Answer by LeathalNinja1986 · Oct 24, 2011 at 03:59 AM

Thanks for the help

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Character does not jump 1 Answer

How do I make realistic jumping? 0 Answers

Changing jump speed so that different height jumps take exactly the same amount of time? 0 Answers

Smoothing out jumping? 1 Answer

Jump on collision problem 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