- Home /
How do I create variable Jump Height for a platformer?
I am trying to put together a platformer style game but am unable to get variable jump height to work. I can jump, I can double jump, but all my jumps are the exact same height. There is no "short hop" version by pressing the jump button and letting go quickly.
I am using C#, the code I am using is below.
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(PlayerPhysics))]
public class PlayerController : MonoBehaviour {
//Player Handling
public float gravity = 20;
public float walkSpeed = 10.0f;
public float runSpeed = 20.0f;
public float acceleration = 30.0f;
public float jumpHeight = 12.0f;
public float jumpAirTime = 0f;
public float jumpMaxAirTime = 0.2f;
public float timeOffGround = 0f;
private float currentSpeed;
private float targetSpeed;
private Vector2 amountToMove;
//sloppy double jump stuff
private bool djump = true; //This must be set to true or false to enable double jump; set to true on item pickup or the like for ingame toggle.
public bool djumpused = true; //checks whether or not the jump has been used; set to true to any spawn off the ground
//will not result in the ability to double jump
private PlayerPhysics playerPhysics;
void Start ()
{
playerPhysics = GetComponent<PlayerPhysics>();
}
void Update ()
{
// Reset acceleration upon collision
if (playerPhysics.movementStopped)
{
targetSpeed = 0;
currentSpeed = 0;
}
if (playerPhysics.grounded)
{
amountToMove.y = 0;
timeOffGround = 0;
djumpused = false;
//Jump
if (Input.GetKeyDown(KeyCode.Space)) // If jump is pressed
{
jumpAirTime = jumpMaxAirTime; //Start jump
jumpAirTime -= Time.deltaTime; //Timer decreases
}
if (amountToMove.y == 0 && Input.GetKeyDown (KeyCode.Space) && jumpAirTime > 0) //If you are jumping
{
amountToMove.y = jumpHeight; //jumping force!
jumpAirTime -= Time.deltaTime; //Timer decreases
}
else
{
amountToMove.y = 0;
jumpAirTime = 0;
timeOffGround =0;
}
}
if (Input.GetKeyDown (KeyCode.Space) && djumpused == false && !playerPhysics.grounded)
{
amountToMove.y = jumpHeight;
djumpused = true;
}
else //gravity takes over at this point
{
timeOffGround += Time.deltaTime;
}
//Input
float speed = (Input.GetButton ("Run"))?runSpeed:walkSpeed;
targetSpeed = Input.GetAxisRaw("Horizontal") * speed;
currentSpeed = IncrementTowards(currentSpeed, targetSpeed,acceleration);
amountToMove.x = currentSpeed;
amountToMove.y -= gravity * Time.deltaTime;
playerPhysics.Move(amountToMove*Time.deltaTime);
}
// Increase n towards target by speed
private float IncrementTowards(float n, float target, float a)
{
if (n == target)
{
return n;
}
else
{
float dir = Mathf.Sign (target - n); //must n be increased or decreainsed to get closer to target
n += a * Time.deltaTime * dir;
return (dir == Mathf.Sign (target-n))? n: target; //if n has increased over target then return target, otherwise return n. The ? n: is a shorthand if statement.
}
}
}
Answer by Hatdatsat · Sep 25, 2013 at 12:24 AM
Maybe make a timer, so if the spacebar is pressed really short like a few milliseconds then it will go x height. and if it's being pressed down long enough go full height. You have a static jump height in this script so it can't change unless you put in the script that it will be more or less over time. Now the script sees press button okay i will do this height, the time of the button is not factor so you need to make it 1. (sorry if my English is bad)
Any advice on the best way to set it to be more or less over time?
I'd like to have a variable jump height rather than two specific jumps (similar to megaman or mario, where they jump until you let go of the button). If I was making a fighter your idea would be great though! :)
I'm afraid i don't have the stomach to down all your code, but you could use something like this...
function Update () // i thinks its void update in c# right?
{
If (Input.Get$$anonymous$$ey(jumpbutton))
{
//increase objects height by an incrament
}else{
//lower the player
}
}
It's in java script ( i'm pretty sure it will still work though) and it might not work with how your are prefor$$anonymous$$g the jumping, but this may be a different way of looking at it, that might work better. sorry if this doesn't help. Good luck
Your answer
![](https://koobas.hobune.stream/wayback/20220613114743im_/https://answers.unity.com/themes/thub/images/avi.jpg)