- Home /
My double jump script is not working
Hi my script isn't working but I cant figure out why. I can't jump in midair which is what I'm going for. I want to be able to jump twice, once from the ground and once in midair. Think Ghouls & Ghosts or Smash Bros. The logic doesn't seem flawed anyway hope some of you can help.
var speed : float = 6.0;
var jump : float = 10.0;
var dJump : float = 7.0;
var gravity : float = 20.0;
private var canDJump : boolean = false;
private var isJumping : boolean = false;
private var isDJumping : boolean = false;
private var velocity : Vector3 = Vector3.zero;
function Update() {
var controller : CharacterController = GetComponent(CharacterController);
if (controller.isGrounded)
{
velocity = Vector3 (Input.GetAxisRaw ("Horizontal"), 0, 0);
velocity = transform.TransformDirection (velocity);
velocity *= speed;
isDJumping = false;
if (Input.GetButtonDown ("Jump"))
{
velocity.y = jump;
isJumping = true;
}
}
if (isJumping == true)
{
canDJump = true;
}
if (canDJump == true && Input.GetButtonDown ("Jump"))
{
velocity.y = dJump;
isDJumping = true;
}
if (isDJumping == true)
{
canDJump = false;
isJumping = false;
}
velocity.y -= gravity * Time.deltaTime;
controller.Move (velocity * Time.deltaTime);
}
You are more likely get useful feedback if you elaborate on "isn't working" (ie, what do you expect to happen and what is actually happening), and if you tidy up the formatting.
Answer by Coderdood · May 31, 2013 at 06:18 AM
The problem is likely that you check the Jump button again immediately after jumping. Since its still the same frame as before, since its the same method, the jump button is still pressed so you double jump immediately.
Your code needs to make sure this isn't all happening in the same frame like so:
var jump : float = 10.0;
var dJump : float = 7.0;
var gravity : float = 20.0;
private var canDJump : boolean = false;
// These two variables become unnecessary
//private var isJumping : boolean = false;
//private var isDJumping : boolean = false;
private var velocity : Vector3 = Vector3.zero;
function Update()
{
// Note: This should really be done in Start as you don't want to waste time every frame getting the controller
var controller : CharacterController = GetComponent(CharacterController);
// By moving this above the jump check you can ensure it only happens on frames after the jump was performed
if (canDJump == true && Input.GetButtonDown ("Jump"))
{
velocity.y = dJump;
//isDJumping = true; // Removed - never used
canDJump = false; // NEW!
}
if (controller.isGrounded)
{
velocity = Vector3 (Input.GetAxisRaw ("Horizontal"), 0, 0);
velocity = transform.TransformDirection (velocity);
velocity *= speed;
//isDJumping = false; // Removed - never used
if (Input.GetButtonDown ("Jump"))
{
velocity.y = jump;
//isJumping = true; // Removed - never used
canDJump = true; // NEW!
}
}
// This block is flawed - you only want to double jump once right?
// Then just set canDJump once the second you start jumping
/*if (isJumping == true)
{
canDJump = true;
}*/
// This block is flawed - just set CanDJump = false the second you start DJumping
/*if (isDJumping == true)
{
canDJump = false;
isJumping = false;
}*/
velocity.y -= gravity * Time.deltaTime;
controller.Move (velocity * Time.deltaTime);
}
Gonna test it but even if it doesnt work I learned a lot like the putting the controller on start and to pay attention to the order of the script and that alone helps me more than getting this double jump to work thanks a lot
worked like a charm except when I put the controller in Function Start() it gives me the console gives me an error. It says unknown identifier for the other times it sees controller in the script which are line 29 and 59.
Also I got the double jump script to work before you answered in my own way but its a mess lol truth be told I'm not 100% sure how I got it.
Your answer
Follow this Question
Related Questions
how to jump with 2d? 1 Answer
Disable script from code 5 Answers
Super mario Camera Script 0 Answers
Script for getting in and out of a tank 2 Answers
Help in Script (Java Script) 0 Answers