Search Unity

Double Jump

Discussion in '2D' started by TheAPGames, Jun 9, 2020.

  1. TheAPGames

    TheAPGames

    Joined:
    Jun 1, 2020
    Posts:
    44
    Hi Everyone,

    I'm trying to figure out this idea of double jump with 2 different animations. My basic double jump works fine as shown here:

    1. Idle
    2. Press jump, switches to jump animation/pose
    3. A double jump is working( but has the same jump pose as jump 1)
    4. lands to idle


    My intention is to have it like this:

    Idle> jump1( jump pose 1) > slight squash > Jump2 ( jump pose 2) > back to idle.

    Here i have my script for the double jump which works fine. Figuring out the rest is confusing me.

    thanks for checking this out and i hope my description is clear.



    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class PlayerController : MonoBehaviour
    {

    public float moveSpeed;
    public Rigidbody2D rb;
    public float jumpForce;
    private bool isGrounded;
    public Transform groundCheckPoint;
    public LayerMask whatIsGround;
    private bool canDoubleJump;
    private Animator anim;



    // Start is called before the first frame update
    void Start()
    {
    anim = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {


    rb.velocity = new Vector2(moveSpeed * Input.GetAxisRaw("Horizontal"), rb.velocity.y);


    isGrounded = Physics2D.OverlapCircle(groundCheckPoint.position, .2f, whatIsGround);

    if (isGrounded)
    {
    canDoubleJump = true;
    }
    if (Input.GetButtonDown("Jump"))

    {
    if (isGrounded) // IF true/WE ARE on the ground, then we can JUMP
    {

    rb.velocity = new Vector2(rb.velocity.x, jumpForce); // IF we press "jump" make the player jump. X is set above, so we just the Y value, which have set in our public float "jumpForce" , which you can in UNITY under Rigidbody2D
    }
    else // if false then do this, must create another IF
    {
    if (canDoubleJump)
    {
    rb.velocity = new Vector2(rb.velocity.x, jumpForce);

    canDoubleJump = false;
    }
    }

    }

    anim.SetBool("isGrounded", isGrounded);
    }
    }