@Komak57
that’s my code to handle double jump.But sometimes it doesn’t work.
First time I touch the screen my character jump up ( variable doublejump = true ) .when I touch the screen a second time my character jump higher(double jump = false)(double jump worked).
But sometimes sometimes I touch the screen for the first time(no need to touch the screen a second time) doublejump variable = false(double jump incorrect work as I expected)
Help me fix that
if (isGrounded) {
doubleJump = false;
}
if (Input.GetMouseButtonDown (0) && this.gameObject.name == "P0") {
if (isGrounded) {
rid.velocity = new Vector3 (0, 50f * jump, 0);
doubleJump = true;
} else {
if (doubleJump) {
rid.velocity = new Vector3 (0, 60f * jump, 0);
doubleJump = false;
}
}
}
Alright, so first up, let me run through this to make sure I know what’s going on. You have a 3D game and you’re using a rigidbody as your player. You want to attach this script to multiple objects that you may take control of at various times. You want to be able to jump pretty high on the first try, and then a little higher on the second, and for some reason, this script doesn’t work every time (more of an issue with the 2nd jump).
Right on line 2 you’re setting doublejump to false. The only instance that should set the double jump, is when you jump for the second time, or when you are about to jump. Remove the first 3 lines and everything should work as expected. My assumption here, is that you’re hitting a frame after the first jump that you’re still classified as grounded, and your jump is being removed.
As far as the gameObject.name goes, never rely on names unless it’s a specific object you want to manage, meaning there’s only ever going to be one of that name (never spawning more). Otherwise, I suggest you use tags or layers. In the case that you have multiple units that gain or lose control, I suggest you JumpScript.setEnabled(false) when you start, and set to true (from another script) when possessing.