- Home /
2D Charge Jump Complications
I have been having trouble getting a charge jump to work. I am a beginner in java script and any help would be helpful. The script doesn't cause any errors but does not cause "jump" when the space key is released.
#pragma strict
public var jumpUpVel : float = 10f;
public var jumpForwardVel : float = 10f;
var Gravity : float = 10f;
var chargeLevel : float = 0;
//Don't change this in the inspector.
var chargeSpeed : float = 1;
//Default, the charge will go up 1 per second
var isCharging = false;
function Update () {
if (Input.GetKey(KeyCode.Space)) { //Did the user click?
if(!isCharging) { //Some what unnecessary due to the way the Input is
// setup
isCharging = true;
CalculateCharge();
}
}
}
function CalculateCharge () {
while(Input.GetKey(KeyCode.Space)) { //Add to the charge as long as the
// user is holding the button
chargeLevel += Time.deltaTime * chargeSpeed;
}
if (Input.GetKeyUp(KeyCode.Space)){
this.gameObject.rigidbody.AddForce(jumpUpVel, jumpForwardVel, 0);
//transform.position.x++;
}
// launch player
// Reset the vars.
chargeLevel = 0.0;
isCharging = false;
}
Answer by syclamoth · Oct 17, 2011 at 03:20 AM
One problem- Input.GetKey(someKey) will never be true on the same frame that Input.GetKeyUp(someKey) is true! This means that the bit inside of calculateCharge that does the launching never actually gets called. The other problem is that CalculateCharge only ever gets called once- since you deactivate the bit that calls it immediately before it gets called the first time.
You're mostly right- you just need to rearrange your code a bit.
Let me show you!
#pragma strict
public var jumpUpVel : float = 10f;
public var jumpForwardVel : float = 10f;
var Gravity : float = 10f;
var chargeLevel : float = 0;
//Don't change this in the inspector.
var chargeSpeed : float = 1;
//Default, the charge will go up 1 per second
// Get rid of this, it's not doing you any good.
//var isCharging = false;
function Update () {
if (Input.GetKey(KeyCode.Space)) { //Is the user Clicking?
chargeLevel += Time.deltaTime * chargeSpeed; // Increase charge
}
if (Input.GetKeyUp(KeyCode.Space)) { // Did the player just stop clicking?
// do the actual jumping here-
// this takes the actual charge level into account-
this.gameObject.rigidbody.AddForce(Vector3(jumpUpVel, jumpForwardVel, 0) * chargeLevel, ForceMode.Impulse);
// now reset the charge level!
chargeLevel = 0;
}
}
A word of warning- using a while loop with variables which are only reset every frame is a good way to crash your game- so this bit of code here-
while(Input.GetKey(KeyCode.Space)) {
//some stuff which will happen forever
}
will put your game into an infinite loop, and freeze it, because Input.GetKey(KeyCode.Space) will only ever evaluate false in the next frame- which your game will never reach, because it is waiting for GetKey(KeyCode.Space) to evaluate false! Which is to say, it is waiting for something to happen, which is in turn waiting for it. So, your program will freeze!
Your answer
Follow this Question
Related Questions
Character does not jump 1 Answer
How do I make realistic jumping? 0 Answers
Changing jump speed so that different height jumps take exactly the same amount of time? 0 Answers
Smoothing out jumping? 1 Answer
Jump on collision problem 0 Answers