- Home /
Double Jump not working
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;
}
}
}
(1) Why did you need to check the name of the gameObject?
(2) Is it possible that doubleJump is being set to false elsewhere?
(3) Where is this code? Is it inside Update? FixedUpdate? a coroutine?
because I have a group of objects including a leader(P0) and member of group (P1, P2 ... Pn), I add scripts for all the objects included in the group.When P0 jumped, members also turn jumping $$anonymous$$y code inside FixedUpdate
Answer by Komak57 · Oct 05, 2015 at 05:00 PM
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.
I $$anonymous$$ake Endless 2D game like zombie Tsunami I have a group of objects including a leader(P0) and member of group (P1, P2 ... Pn), I add scripts for all the objects included in the group.When P0 jumped, members also turn jumping All $$anonymous$$y Code:
if (isGrounded) {
doubleJump = false;
jumpAnim = false;
if (gm.playerState == Game$$anonymous$$anager.PlayerState.normalState) {
anim.SetBool ("doubleJump", false);
} else if (gm.playerState == Game$$anonymous$$anager.PlayerState.$$anonymous$$idas$$anonymous$$ingState) {
anim.SetBool ("midasDoubleJump", false);
} else if (gm.playerState == Game$$anonymous$$anager.PlayerState.NinjaState) {
anim.SetBool ("ninjaDoubleJump", false);
}
} else {
jumpAnim = true;
}
if (target == null) {
if (isGrounded) {
if (Input.Get$$anonymous$$ouseButtonDown (0)) {
rid.velocity = new Vector3 (0, 50f * jump, 0);
doubleJump = true;
}
} else {
if (doubleJump) {
if (Input.Get$$anonymous$$ouseButtonDown (0)) {
rid.velocity = new Vector3 (0, 60f * jump, 0);
if (gm.playerState == Game$$anonymous$$anager.PlayerState.normalState) {
anim.SetBool ("doubleJump", true);
} else if (gm.playerState == Game$$anonymous$$anager.PlayerState.$$anonymous$$idas$$anonymous$$ingState) {
anim.SetBool ("midasDoubleJump", true);
} else if (gm.playerState == Game$$anonymous$$anager.PlayerState.NinjaState) {
anim.SetBool ("ninjaDoubleJump", true);
}
doubleJump = false;
}
}
}
}
if (target != null && isGrounded && target.GetComponent<$$anonymous$$ygroup> ().doubleJump && !target.GetComponent<$$anonymous$$ygroup> ().isGrounded && dir <= 2f) {
StartCoroutine (Jump ());
}
if (target != null && !isGrounded && doubleJump && !target.GetComponent<$$anonymous$$ygroup> ().isGrounded
&& !target.GetComponent<$$anonymous$$ygroup> ().doubleJump && dir <= 2f) {
StartCoroutine (JumpDouble ());
}
Again, stop setting doubleJump = false; on line 2. doubleJump should ONLY be set to true when grounded, and set to false when jumping while airborn.
if (isGrounded) {
jumpAnim = false; // stop jump animation
}
if (target == null) {
if (isGrounded) {
if (Input.Get$$anonymous$$ouseButtonDown (0)) {
rid.velocity = new Vector3 (0, 50f * jump, 0);
doubleJump = true; // reset double jump
jumpAnim = true; // animate
}
} else {
if (doubleJump) {
if (Input.Get$$anonymous$$ouseButtonDown (0)) {
rid.velocity = new Vector3 (0, 60f * jump, 0);
doubleJump = false; // consume double jump
}
}
}
}
// evaluate animation changes EVERY update, not just when the event occurs
if (gm.playerState == Game$$anonymous$$anager.PlayerState.normalState) {
anim.SetBool ("doubleJump", doubleJump && !isGrounded); // evaluate to is double jumping
} else if (gm.playerState == Game$$anonymous$$anager.PlayerState.$$anonymous$$idas$$anonymous$$ingState) {
anim.SetBool ("midasDoubleJump", false); // TODO: evaluate midas double jumping
} else if (gm.playerState == Game$$anonymous$$anager.PlayerState.NinjaState) {
anim.SetBool ("ninjaDoubleJump", false); // TODO: evaluate ninja double jumping
}
if (target != null && isGrounded && target.GetComponent<$$anonymous$$ygroup> ().doubleJump && !target.GetComponent<$$anonymous$$ygroup> ().isGrounded && dir <= 2f) {
StartCoroutine (Jump ());
}
if (target != null && !isGrounded && doubleJump && !target.GetComponent<$$anonymous$$ygroup> ().isGrounded
&& !target.GetComponent<$$anonymous$$ygroup> ().doubleJump && dir <= 2f) {
StartCoroutine (JumpDouble ());
}
Your answer
Follow this Question
Related Questions
Hosting a server at home 4 Answers
How can you detect the oculus home overlay without the OVR manager? 0 Answers
[Solved]handle android hardware buttons 1 Answer
How to play Unity on Android's background Home Screen? 1 Answer
HTC, and other Androids with sense UI reloads sense UI when the home button is hit 0 Answers