Question by
PunchGoat · Mar 21, 2016 at 01:26 AM ·
javascriptjumpingdouble jump
My player is double jumping when it should not be able to. I am not sure what is allowing it to do so.
I wrote a script to allow my player to perform a "charged" jump. The longer the space bar is held down the more power is added to the minimum jump. The code is working exactly as it should be, but it's also allowing a double jump. The air jumping is not infinite. It's limited to only one jump, but I still need to remove it. How do I allow the player to jump only when touching the ground?
var onGround :boolean = false;
var jumpPressure : float = 0;
var minJump : float = 5;
var maxJumpPressure : float = 10;
var rigid : Rigidbody;
var maxAngle = 80;
function Start () {
rigid = GetComponent.<Rigidbody>();
}
function Update () {
// If the player is on the ground
if(onGround == true) {
// If the player presses jump
if(Input.GetKey("space")){
// Begin building Pressure
if (jumpPressure < maxJumpPressure){
jumpPressure += Time.deltaTime * 10;
}
// Once the pressure is at the max, keep it there
else {
jumpPressure = maxJumpPressure;
}
}
// If the player isn't pressing jump
else {
if (jumpPressure > 0) {
jumpPressure = jumpPressure + minJump;
rigid.velocity = new Vector3 (0, jumpPressure, 0);
jumpPressure = 0;
onGround = false;
}
}
}
}
// Check if the player is on the ground
function OnCollisionStay (collision : Collision) {
for (var contact : ContactPoint in collision.contacts) {
if (Vector3.Angle(contact.normal, Vector3.up) < maxAngle) {
onGround = true;
}
}
}
Comment
OnCollisionStay is probably true for the frame inbetween the actual jump and you setting onGround = false. Try using OnCollisionEnter ins$$anonymous$$d.
Your answer
Follow this Question
Related Questions
Player not double jumping. 0 Answers
player not able to double jump 0 Answers
jumping once if touching the ground js 0 Answers
Jump Script not Working JavaScript 1 Answer
Player jumping issue 0 Answers