I’ve been making a platformer for some time, and the player can both jump and have an upwards force applied so the player can hover.
However, when I press jump, then immediately press gravity, the forces are added together and the player goes a lot higher than intended, and this would break the game in make instances.
How can I detect that I’m jumping so that if the player presses the gravity button moments later the two forces aren’t added together?
I’ve copied in the code for the problem:
function FixedUpdate () {
if(grounded == true){
if (jumpKeyPressed){
if(inwater == false){
rigidbody.velocity.y += jumpSpeed;
}else{
rigidbody.velocity.y -= (jumpSpeed-(jumpSpeed/3));
}
}
}
if(Gravity == false){
Physics.gravity = Vector3(0, gravity, 0);
}
if(Gravity == true){
Physics.gravity = Vector3(0, -gravity, 0);
}
jumpKeyPressed = false;
if (rigidbody.IsSleeping()){
rigidbody.WakeUp();
}
grounded = false;
}
Any ideas would be appreciated.