- Home /
Question by
Segy · Feb 08, 2016 at 05:21 PM ·
script.jumpplatformerdouble jump
Can not double jump when i fall off platform
Below is my script. I am making a 2D platform running game. My player is able to do double jump. However, when I fall off the edge of the platform i can only do 1 jump.
My script is only saying to double jump when I am grounded. I want to be able to do double when I am grounded and when i am not.
Thankyou
void Update()
{
grounded = Physics2D.IsTouchingLayers(myCollider, whatIsGround);
myRigidbody.velocity = new Vector2(moveSpeed, myRigidbody.velocity.y);
if (Input.GetMouseButtonDown(0))
{
if (grounded)
{
myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, jumpForce);
stoppedJumping = false;
}
if (!grounded && canDoubleJump)
{
myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, jumpForce);
jumpTimeCounter = jumpTime;
stoppedJumping = false;
canDoubleJump = false;
}
}
if ((Input.GetMouseButton(0)) && !stoppedJumping)
{
if (jumpTimeCounter > 0)
{
myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, jumpForce);
jumpTimeCounter -= Time.deltaTime;
}
}
if (Input.GetMouseButtonUp(0))
{
jumpTimeCounter = 0;
stoppedJumping = true;
}
if (grounded)
{
jumpTimeCounter = jumpTime;
canDoubleJump = true;
}
}
Comment
Answer by Charlie122 · Feb 06, 2019 at 09:33 PM
public float jumpHeight = 100;
public bool groundCheck;
private const int Max_Jump = 2;
private int currentJump = 0;
public Rigidbody rb;
void start (){
rb = GetComponent<Rigidbody> ();
}
void Update ()
{
if (Input.GetButtonDown ("Jump") && (groundCheck || Max_Jump > currentJump)) {
rb.velocity = new Vector3 (0, jumpHeight );
groundCheck = false;
currentJump++;
{
}
}
}
void OnCollisionEnter (Collision other){
if (other.gameObject.tag == "Ground") {
groundCheck = true;
currentJump = 0;
}
}
}
Your answer
Follow this Question
Related Questions
Which is best for optimization 1 Answer
2D Platformer AI Jump? 2 Answers
How to determine if the player can jump, without using raycasts. (2D) 1 Answer
Character won't jump. 0 Answers
I'm having trouble getting my character to jump (2D) 1 Answer