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 mkjrfan · Jan 14, 2013 at 10:26 PM · movementjumpcontrol

Trying to get accelerated jump speed.

Hi, I am trying to make the player jumps controllable by allowing the player determine how high the player jumps. Basically if the player holds the jump button longer the higher the player goes until it reaches the max speed.

This is my Code:

 void Update () {
 
 checkMovement();
 HandleActionInput();
 processMovement();
 }
 
 void HandleActionInput(){
     if(Input.GetButtonDown("Jump")){
             jump();
             inAir = true;
         }
 }
 
 
 public void jump(){
     if(canJump==true)
         {
         VerticalVelocity += JumpSpeed;
         }
     if (VerticalVelocity>=maxJump){
         canJump=false;    
         }
     }

The problem is that it wont rise any higher when pressing and holding the jump button. Why does this happen?

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 Piflik · Jan 14, 2013 at 10:35 PM

Because GetButtonDown() is only true for one single frame after the button is pressed. If you want to hold a button, use GetButton().

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 mkjrfan · Jan 14, 2013 at 10:47 PM 0
Share

Thank you!

avatar image cdrandin · Jan 14, 2013 at 11:03 PM 0
Share

ha, didn't even notice that. They are sneaky little things.

avatar image
0

Answer by cdrandin · Jan 14, 2013 at 10:34 PM

Well, you are missing a lot of info for anyone to help you. No deceleration so idk what type you dealing with.

You can use my own code which is in Boo, very easy to transfer to unityscript. Keep in mind "single" means float. It is also assuming 2D physics which 'w' and 'spacebar' are defined as jump inputs.

 import UnityEngine
 
 [AddComponentMenu("Character/TwoDPlatformController")]
 [RequireComponent( CharacterController )]
 
 class TwoDPlatformController ( MonoBehaviour ): 
     //public     test  = TwoDPlatformControllerJumping()  Access another class will also appear in inspector
     public walkSpeed   as single     = 6.0F
     public runSpeed       as single     = 10.0F
     public jumpSpeed   as single     = 8.0F
     public gravity     as single     = 20.0F
     public doubleJumpEnabled  as bool= true
     public extraJump as int          = 1
     public autoRotate  as bool       = true
     public maxRotationSpeed as single = 360
     
     private moveDirection as Vector3 = Vector3.zero
     private curNumJump    as int     = 0
     private curSpeed      as single  = 0.0F
     
     internal jumped       as bool    = false
     
     private jumpingApexReached as bool = false
     
     private controller as CharacterController
 
     
     def Update():
         PlayerMovement()
         HandleEvents()
         
         
     def PlayerMovement ():
         controller = GetComponent(CharacterController)
         
         // While character is grounded allow to jump and horizontal movement
         // Player is restricted in jump movement.  One way jump if double jump disabled
         if controller.isGrounded:
             curNumJump = 0
             jumped = false
             
             // Assign run speed when pressing left ctrl
             if Input.GetKey(KeyCode.LeftControl):
                 curSpeed = runSpeed
                 
             // Assign default walk speed
             else:
                 curSpeed = walkSpeed
                 
             // Apply vector direction, only focusing on X- axis
             moveDirection = Vector3( Input.GetAxis('Horizontal') * curSpeed, 0, 0 )
             
             // If player has jumped
             // Allow player to single jump while in air
             // Check if player has reached jump apex, used for animation states
             if hasSingleJumped():
                 SingleJump()
                 hasReachedJumpingApex()
         
         // Player is in the air
         // Allow the player the option of extra jumping, already checks if extra jumping is enabled
         // Check if player has reached jump apex, used for animation states
         // TODO: Future implementation, allow user to shoot while in air
         else:
             if hasExtraJumped():
                 ExtraJump()
                 hasReachedJumpingApex()
             
         
         ApplyGravityForce()
 
         // Apply new vector direction to the character controller
         controller.Move( moveDirection * Time.deltaTime )
         
     def SingleJump ():
     // Immediately present number of allowed mid air jumps when landed
     // Allow player to jump
         moveDirection.y = jumpSpeed
         jumped = true
             
     def ExtraJump ():
     // Allow player the capability to jump multiple times in mid air
     //  Number of times is based on 'extraJump'
     //  Once limit is reach player must land before jumping again
     // When jumping horizontal movement is unlocked for that jump then it will be locked again
     
         if curNumJump < extraJump:
             moveDirection = Vector3( Input.GetAxis('Horizontal') * walkSpeed, 0, 0 )
             moveDirection.y = jumpSpeed
             curNumJump += 1    
             
     def ApplyGravityForce ():
         moveDirection.y -= ( gravity * Time.deltaTime )
         
     def HandleEvents ():
     // Events taken place are of action types: shooting, special attacks, interactable object, etc
         pass
         
     def hasSingleJumped ():
         return ( Input.GetButtonDown( 'Jump' ) or Input.GetKeyDown( 'w' ) ) 
         
     def hasExtraJumped  ():
         return ( Input.GetButtonDown( 'Jump' ) or Input.GetKeyDown( 'w' )  and doubleJumpEnabled )
         
     def hasReachedJumpingApex ():
         if jumped and controller.velocity.y <= 0.0:
             jumpingApexReached = true
             return jumpingApexReached
         
         else:
             if controller.isGrounded:
                 jumpingApexReached = false
                 return jumpingApexReached
         
     def getSpeed ():
         return moveDirection.x

You can remove AddComponentMenu part. Change [RequireComponent..] to @script RequireComponent(..)

This uses the CharacterControllr provide by Unity which is a way of control the player without the interruption of physics to provide unique movements.

Comment
Add comment · Show 3 · 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 cdrandin · Jan 14, 2013 at 10:34 PM 0
Share

If you provide your entire code I can probably help you out more.

avatar image mkjrfan · Jan 14, 2013 at 10:49 PM 0
Share

Sorry I just didn't think that I needed to put in some variable definition like jumpSpeed since it doesn't really matter what it is as long as it's greater than zero.

avatar image cdrandin · Jan 14, 2013 at 11:00 PM 0
Share

it is just giving full control over to the programmers to how specific they want the player to be. it is mainly for convenience and a good practice.

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

10 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

Related Questions

How to make camera position relative to a specific target. 1 Answer

In air movement troubles. 1 Answer

tilt control for jump 0 Answers

Disable movement control during jump 3 Answers

About in air control 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