- Home /
Trouble with Jumping Algorithm
I'm working on a pretty basic platformer right now, and I'm having a lot of trouble getting jumping to work the way I want it to. right now the physics (speed and height) of the jump are pretty much where i want them to be, but the problem is that one out of every 3 or 4 times the player pressed the jump button they just don't jump. I have tried a lot of things to get this to work, right now the way its set up is there are trigger colliders that go just a bit above the surface of the ground, and when the player enters them the 'jumping' boolean gets set to false which means the player can jump again. before I had it rigged up such that if the player isn't moving on the y axis then they can jump. now if i have it set up so that if either the collider has been entered or they aren't moving in the y axis then they can jump. Honestly I'm not sure what else to try, does anyone have any ideas? here is my code
#pragma strict
//movement speed and jumping force set within the editor
public var moveSpeed : float;
public var jumpForce : float;
//keeps track of whether or not the player is jumping
public static var jumping : boolean = false;
public static var lives : int; // no longer recorded
//is the player dead?
public static var dead : boolean;
//players y velocity
private var jumpNumb : float;
var animationChild : Animator;
public var eyeChild : SpriteRenderer;
function Start () {
animationChild = GetComponentInChildren(Animator) as Animator;
moveSpeed = 7;
jumpForce = 1.25;
dead = false;
lives = 5;
}
function FixedUpdate () {
transform.localEulerAngles = new Vector3(0,0,0);
//move character along vertical axis baed on horizontal input, time, and movement speed
transform.Translate(Input.GetAxis("Horizontal") * -moveSpeed * Time.deltaTime,0,0);
//flips the character based on which direction they are moving
if(Input.GetAxis("Horizontal") * 10 < 0){
transform.localScale.x = -1;
} else if (Input.GetAxis("Horizontal") > 0){
transform.localScale.x = 1;
}
//NOTE: sometimes the movement in the y value is a ridiculously small decimal, this is for rounding purposes.
if((rigidbody2D.velocity.y < 0 && rigidbody2D.velocity.y > -0.05) || (rigidbody2D.velocity.y > 0 && rigidbody2D.velocity.y < 0.05)){
jumpNumb = 0;
} else {
jumpNumb = rigidbody2D.velocity.y;
}
if(Input.GetKeyDown(KeyCode.Space)){
Debug.Log("Im jumping, " + jumping);
}
//if the player has hit the jump button, and if they either have hit a jump collider, or if they arent moving in the y axis, they can jump again.
if(Input.GetKeyDown(KeyCode.Space) && (jumping == false || jumpNumb == 0))
{
jumping = true;
rigidbody2D.velocity.y = -Physics.gravity.y * jumpForce;
}
if(dead){
deathCycle();
}
}
function deathCycle() {
//kills the player and resets the level
animationChild.SetInteger("dead", 1);
lives = lives - 1;
eyeChild.enabled = false;
DoorWarp.unlocked = false;
DoorUnlocker.keyCollected = false;
dead = false;
yield WaitForSeconds(1);
Application.LoadLevel(Application.loadedLevel);
}
Answer by Winterblood · Apr 27, 2014 at 10:28 PM
I suggest when the player presses SPACE you do a tiny raycast down to see if they have solid ground to kick off from. That should work regardless of velocity. Details here:
http://docs.unity3d.com/Documentation/ScriptReference/Physics2D.Raycast.html
And a couple of more advanced tricks that may help with the feel of the game - most platformers will store the time the JUMP button was last pressed, and if you're not allowed to jump right that moment (eg. you are about to hit the ground but pressed SPACE too early) it will keep checking for 0.1 seconds and trigger a jump if you hit the ground within that short delay. And also they will usually keep an "OnGround" bool active for 0.1 seconds after dropping off a platform, so that a fractionally late press of SPACE still allows you to jump. Both of these are basically being a little bit generous to the player, so that when they miss a jump they feel like it's THEIR mistake, not your game's :)
Your answer
Follow this Question
Related Questions
How to make Jump 2D Action Stop after finite amount of Time? 1 Answer
Can't implement jumping to 2D side-scroller click-to-move. 1 Answer
Raycast2D not working 1 Answer
Problem with Jump 1 Answer
2D game with depth, basics? 2 Answers