Search Unity

CharacterMotor - how can I keep the Jumping go higher until jumpkey is released?

Discussion in 'Scripting' started by khos85, Aug 30, 2015.

  1. khos85

    khos85

    Joined:
    Jul 21, 2013
    Posts:
    541
    Hi,

    I would like to ask for advice on this please, I have been searching the web etc but so far not come up with anything similar (maybe my searches are not good enough?)

    I'm using Unity 5.1 and my player has CharacterMotor and the FPSInputController scripts attached, It all works well but I'd like to have the option where if I hold the Jump key down, when that happens then the character should just keep going up until the jump key is released.. as if I have a jetpack which can make the player fly up high.. I have tried going through these scripts but am not sure how to modify this for my needs. Can anyone give me a clue on where to start looking, or how to change it?

    I think (of course I might be wrong) but the jump code is controlled in CharacterMotor.js , specifically this piece of code:


    Code (JavaScript):
    1. private function ApplyGravityAndJumping (velocity : Vector3) {
    2.  
    3.     if (!inputJump || !canControl) {
    4.         jumping.holdingJumpButton = false;
    5.         jumping.lastButtonDownTime = -100;
    6.     }
    7.  
    8.     if (inputJump && jumping.lastButtonDownTime < 0 && canControl)
    9.         jumping.lastButtonDownTime = Time.time;
    10.  
    11.     if (grounded)
    12.         velocity.y = Mathf.Min(0, velocity.y) - movement.gravity * Time.deltaTime;
    13.     else {
    14.         velocity.y = movement.velocity.y - movement.gravity * Time.deltaTime;
    15.  
    16.         // When jumping up we don't apply gravity for some time when the user is holding the jump button.
    17.         // This gives more control over jump height by pressing the button longer.
    18.         if (jumping.jumping && jumping.holdingJumpButton) {
    19.             // Calculate the duration that the extra jump force should have effect.
    20.             // If we're still less than that duration after the jumping time, apply the force.
    21.      
    22.             //
    23.             if (Time.time < jumping.lastStartTime + jumping.extraHeight / CalculateJumpVerticalSpeed(jumping.baseHeight)) {
    24.                 // Negate the gravity we just applied, except we push in jumpDir rather than jump upwards.
    25.                 velocity += jumping.jumpDir * movement.gravity * Time.deltaTime;
    26.             }
    27.         }
    28.  
    29.         // Make sure we don't fall any faster than maxFallSpeed. This gives our character a terminal velocity.
    30.         velocity.y = Mathf.Max (velocity.y, -movement.maxFallSpeed);
    31.     }
    32.  
    33.     if (grounded) {
    34.         // Jump only if the jump button was pressed down in the last 0.2 seconds.
    35.         // We use this check instead of checking if it's pressed down right now
    36.         // because players will often try to jump in the exact moment when hitting the ground after a jump
    37.         // and if they hit the button a fraction of a second too soon and no new jump happens as a consequence,
    38.         // it's confusing and it feels like the game is buggy.
    39.         if (jumping.enabled && canControl && (Time.time - jumping.lastButtonDownTime < 0.2)) {
    40.             grounded = false;
    41.             jumping.jumping = true;
    42.             jumping.lastStartTime = Time.time;
    43.             jumping.lastButtonDownTime = -100;
    44.             jumping.holdingJumpButton = true;
    45.      
    46.             // Calculate the jumping direction
    47.             if (TooSteep())
    48.                 jumping.jumpDir = Vector3.Slerp(Vector3.up, groundNormal, jumping.steepPerpAmount);
    49.             else
    50.                 jumping.jumpDir = Vector3.Slerp(Vector3.up, groundNormal, jumping.perpAmount);
    51.      
    52.             // Apply the jumping force to the velocity. Cancel any vertical velocity first.
    53.             velocity.y = 0;
    54.             velocity += jumping.jumpDir * CalculateJumpVerticalSpeed (jumping.baseHeight);
    55.      
    56.             // Apply inertia from platform
    57.             if (movingPlatform.enabled &&
    58.                 (movingPlatform.movementTransfer == MovementTransferOnJump.InitTransfer ||
    59.                 movingPlatform.movementTransfer == MovementTransferOnJump.PermaTransfer)
    60.             ) {
    61.                 movement.frameVelocity = movingPlatform.platformVelocity;
    62.                 velocity += movingPlatform.platformVelocity;
    63.             }
    64.      
    65.             SendMessage("OnJump", SendMessageOptions.DontRequireReceiver);
    66.         }
    67.         else {
    68.             jumping.holdingJumpButton = false;
    69.         }
    70.     }
    71.  
    72.     return velocity;
    73. }
    I could not see how to format the code in my post here , apologies for that.
    I tried changing various parts in this code e.g. remove "Time.time < jumping.lastStartTime" but nothing helps me so far.
    Any help would be much appreciated.
     
    Last edited: Aug 30, 2015
  2. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Use code tags.
     
  3. khos85

    khos85

    Joined:
    Jul 21, 2013
    Posts:
    541
    Many thanks hippocoder, found the option after a bit more searching.
     
  4. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    I have to point out to people to use code tags because it dramatically increases the chance that an experienced programmer will look at it.
     
  5. khos85

    khos85

    Joined:
    Jul 21, 2013
    Posts:
    541
    Agreed :) Thanks for helping.
     
  6. khos85

    khos85

    Joined:
    Jul 21, 2013
    Posts:
    541
    I would like to give this a bump up.