- Home /
double jump
Hi guys I'm working on a 2d platform game and i've got the code from the unity tutorial "Lerpz". Now I want my character to do a double jump. You press jump once... he jumps normal. When you press it while he's jumping normal again you do a frontflip. Here is the code.. I already tried some things but nothing worked:
Answer by DavidDebnar · Oct 17, 2011 at 09:28 PM
Hi, I redid and tested the code and it works perfectly. Feel free to tweak the values.
#pragma strict
@script RequireComponent(Rigidbody); //forces this gameObject to also have a rigibody component
//public variables
var maxAirJumpCount : int = 1; // 1 for a double jump
var normalJumpForce : float = 8; //strength of the normal jump's impulse
var airJumpForce : float = 5; //strength of the double jump's impulse
var raycastDistance : float = 1; //distance between the characters origin and the ground, 1 for a 2m high character
var xSpeed : float = 10; // horizontal speed of the character
/* These values are all ideal settings that worked for me */
//private variables
private var airJumpCount : int = 0; //how many more times can the player jump?
private var xMove : float; // horizontal input (Input.GetAxisRaw("Horizontal")) *
// horizontal speed modifier (xSpeed)
private var isGrounded : boolean = false; //is the player on the ground?
function Update() {
if(Physics.Raycast(transform.position, -Vector3.up, raycastDistance)) {
isGrounded = true;
airJumpCount = maxAirJumpCount;
}
else {
isGrounded = false;
}
if(Input.GetKeyDown(KeyCode.Space)) {
if(isGrounded) {
JumpNormal();
}
else if(airJumpCount > 0) {
airJumpCount -= 1;
JumpInAir();
}
}
xMove = 0;
xMove = Input.GetAxisRaw("Horizontal") * xSpeed;
}
function FixedUpdate () {
rigidbody.AddForce(Vector3.right*xMove);
}
function JumpNormal () { rigidbody.AddForce(Vector3.up * normalJumpForce, ForceMode.Impulse); }
function JumpInAir () { rigidbody.AddForce(Vector3.up * airJumpForce, ForceMode.Impulse); }
---David
thats all or do I have to wirte it in my script?
This is just a pseudo code that shows you how to make it. Just add a JumpNorm() and JumpDouble() functions that will do the jump and double jump and it will work.
yes exactly that's the thing what things are in these functions? The code is in the link I#m just too stupid to do this. Usualy I can write every script I want but this time I#m just kind of... stupid. Sorry... please!
Sorry, but I don't have time to read your script, but just do this. If function in which you make the char jump add the things above. Then declare the vars at the start and make another two function JumpNorm() and JumpDouble(). Then add your usuall jump code to JumpNorm() and your double jump code to JumpDouble().
$$anonymous$$
private **e**var grounded : boolean = false;
did you men private var grounded : boolean = false;
Answer by bubblegumsoldier · Oct 19, 2011 at 07:05 PM
OHH It worked if somebody wants the script here's the link
Things I would like: Edit your questions and add a link to the solutions in there, and tick the correct answer/close the question.
but maybe somebody want to add something later at this question and has a script that's better or so