Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Trying to figure out how some code works

FallFall Member
edited December 2021 in Programming

So basically, I did a tutorial on how to make 2d player movement and double jumping in unity. https://www.youtube.com/watch?v=QGDeafTx5ug&list=PLBIb_auVtBwBotxgdQXn2smO0Fvqqea4-&index=1

I'm trying to figure out how it generally works because he doesn't explain it too well in the video. It works as it should (if I set extra jumps to one, I can double jump, 2, I can triple jump, etc etc).


I'm confused though, because in my print statement (in Update in the second if statement) where it prints 'extra jumps', it:

(in this situation my extraJumpsValue is 2, so a triple jump)

prints 2 on my first jump (off of the ground)

2 on my second jump - even though it should have subtracted extrajumps by 1 already)

and 1 on my third.


I don't really understand why or how it works like that.

If anyone's able to link me a resource or just explain to me in general how it works that would be nice.

Thanks!

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


public class PlayerMovement : MonoBehaviour {


    public float speed;
    public float jumpForce;
    private float moveInput;


    private Rigidbody2D rb;


    private bool facingRight = true;
    private bool isGrounded;
    public Transform groundCheck;
    public float checkRadius;
    public LayerMask whatIsGround; // put all ground objects in ground layer




    private int extraJumps; // adds on to the default one jump
    public int extraJumpsValue; 


    void Start() // runs on start
    {
        extraJumps = extraJumpsValue;
        rb = GetComponent<Rigidbody2D>();
    }


    void FixedUpdate() // best for managing physics
    {


        isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGround);
        // first one is pos, second is radius, third is layer mask


        moveInput = Input.GetAxis("Horizontal"); // getaxisraw makes movement more snappy
        rb.velocity = new Vector2(moveInput * speed, rb.velocity.y); // first one is x, second is y


        if (facingRight == false && moveInput > 0) // checks if moving right while facing left
        {
            Flip();
        } else if (facingRight == true && moveInput < 0) // checks opposite
        {
            Flip();
        }
    }


    void Update()
    {
        if (isGrounded == true)
        {
            extraJumps = extraJumpsValue;
        }


        if (Input.GetKeyDown(KeyCode.Space) && extraJumps > 0)
        {
            print(extraJumps);
            rb.velocity = Vector2.up * jumpForce;
            extraJumps --; // -1
        }
        else if (Input.GetKeyDown(KeyCode.Space) && extraJumps == 0 && isGrounded == true)
        {
            rb.velocity = Vector2.up * jumpForce;
        }


    }


    void Flip()
    {
        facingRight = !facingRight; // negate facingRight - true becomes false, false becomes true
        Vector3 scaler = transform.localScale; // player's x y and z scale values
        scaler.x *= -1; // flips x
        transform.localScale = scaler; // apply flipping
    }
}
>

UPDATE: Apparently it runs that top if statement in the update function, resetting it to 2. Shouldn't it not run that if my player is still in the air, though?

Sign In or Register to comment.