Double jump. Jumping after falling off a platform
I have tried to make a double jumping feature in my game. It works fine. But. A player can jump after leaving a platform if a jump is done. If he uses double jump before he lands, he can't jump after leaving a platform. In my game I don't want a Player to be able to jump if he falls off a platform.
using UnityEngine; using System.Collections;
public class PlayerController : MonoBehaviour {
public float moveSpeed;
public float jumpForce;
public bool grounded;
public LayerMask whatIsGround;
private Rigidbody2D rb;
private Collider2D coll;
private Animator anim;
private bool canDoubleJump;
void Start ()
{
rb = GetComponent<Rigidbody2D> ();
coll = GetComponent<Collider2D> ();
anim = GetComponent<Animator> ();
}
void FixedUpdate ()
{
grounded = Physics2D.IsTouchingLayers (coll, whatIsGround);
}
void Update ()
{
rb.velocity = new Vector2 (moveSpeed, rb.velocity.y);
if (Input.GetButtonDown ("Fire1"))
{
if (grounded)
{
rb.velocity = new Vector2 (rb.velocity.x, jumpForce);
canDoubleJump = true;
}
if (!grounded && canDoubleJump)
{
rb.velocity = new Vector2 (rb.velocity.x, jumpForce);
canDoubleJump = false;
}
}
anim.SetFloat ("Speed", rb.velocity.x);
anim.SetBool ("Grounded", grounded);
}
}
My guess is to somehow reset a "canDoubleJump" after a Player lands but everything I tried didn't work. So. I need some help. I am a begginer in scripting and Unity as you can obviously see. And my English is not that well) Thanks!
Answer by ArturoSR · Sep 09, 2016 at 12:12 PM
Hello there.
OK, you can make the change when the player lands into the ground, he can jump after that, now this is the trick:
If the character it has "touched" any ground layered as "environment" he can make a double jump.
But, if the character it has "touched" any ground layered as "platforms" then he can't make any jump.
So, what you already need to do it is store the layer value to make a comparison at the jump event, something like this:
if (grounded) {
if ("The current ground equal to environment") {
"Then he can jump and double jump";
}
} else {
"Here makes the character go down to ground";
}
As you can see, only, if the character is the ground established to make a jump he then can jump, but, if not, then he deserves to die (uuuuooooh! you are so bad), cheers.
Thanks a lot for the answer, but I did't really understand you. Or, maybe, you didn't understand me) I think you wrote about two types of "ground"/"platforms"/"enviroment". Like I have "Platform_1" where a Player can jump/double jump, and "Platform_2" where he cannot. That's not my case. I have only one type of platforms and they are layered "ground". I repeat, maybe I didn't understand you correctly) If so, then I'll continue thinking of how to use your answer.
Another situation is if YOU didn't understand me. Well, my fault. It is hard to explain, so I'll just show you) ![alt text][1] [1]: /storage/temp/77935-12121.png
We have 4 positions. 1. A player is running. 2. A player is jumping to reach the second platform. If a player doesn't use double jump then he lands on a platform and "CanDoubleJump" is true. So when he runs off the second platform (position 4), he can use his double jump and saves himself. I don't want to have this feature) It may not be bad, but in my game it is like a cheat for me. 2a. A player makes double jump. If he used double jump then "CanDoubleJump" is false. So when he runs off the second platform he is doomed.
Any ideas?
O$$anonymous$$, another option will be to check if the player is jumping while is grounded and not falling (need to set a field/variable that allow you to make this comparison, this is because the double jump option).
Your answer
Follow this Question
Related Questions
make rigidbody jump and maintain velocity 1 Answer
Jumping to platform of different depth in 3D game 0 Answers
What am i doing wrong with my jump attack ? 0 Answers
How to double jump? i just can jump once when it touch the ground.. help me guys =D 1 Answer
How can i combine Coyote Time and Jump Buffering with double jump? 0 Answers