- Home /
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?
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().
ha, didn't even notice that. They are sneaky little things.
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.
If you provide your entire code I can probably help you out more.
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.
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
Follow this Question
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