Unity 2D Jump script

Alpha

So I was trying to implement double jumping in my game, which doesn't work. And now, somehow, not only can't my players double jump, they can't even jump either!

update: they can jump now, still can't double jump though.

This is my whole movement script:

using UnityEngine;

namespace Players
{
    public class Actor : MonoBehaviour
    {
        //in order to control both players using 1 script.
        public int playerIdx;

        //Variables.
        public float movementSpeed = 150f;
        public float jumpForce = 250f;

        //Ground stuff.
        public LayerMask whatIsGround;
        public bool grounded;

        //boolean stuff.
        private bool facingRight;
        private bool moving;

        //Needed to check if player is on the ground.
        public Transform groundCheck;

        //Limit player's movement speed.
        public float maxMovementSpeed = 400f;

        //Double jump stuff.
        private bool doubleJumpReady;

        //rb
        private Rigidbody2D rb;


        // Start is called before the first frame update
        void Start()
        {
            doubleJumpReady = true;
            rb = GetComponent<Rigidbody2D>();
            facingRight = true;
        }

        // Update is called once per frame
        void FixedUpdate()
        {
            SlowDown();
        }

        private void LateUpdate()
        {
            grounded = Physics2D.OverlapCircle(groundCheck.position, 0.1f, whatIsGround);

            if (grounded)
                doubleJumpReady = true;           

        }

        private void SlowDown()
        {

            if (moving) return;

            //if player is not moving, slow them down.
            if (rb.velocity.x > 0.2f)
                rb.AddForce(movementSpeed * Time.deltaTime * -Vector2.right);
            if (rb.velocity.x < -0.2f)
                rb.AddForce(movementSpeed * Time.deltaTime * Vector2.right);
        }

        public void Move(int dir)
        {
            //Flip the player.
            Flip(dir);

            //Moving the player.
            moving = true;

            float xVel = rb.velocity.x;            //Get x velocity.

            if ( dir > 0)
                rb.AddForce(movementSpeed * Time.deltaTime * Vector2.right * dir);
            else if (dir < 0)
                rb.AddForce(movementSpeed * Time.deltaTime * Vector2.right * dir);
            else if (dir == 0) { }  //do nothing.

            //Help player turn around faster.
            if (xVel > 0.2f && dir < 0)
                rb.AddForce(movementSpeed * 3.2f * Time.deltaTime * -Vector2.right);
            if (xVel < 0.2f && dir > 0)
                rb.AddForce(movementSpeed * 3.2f * Time.deltaTime * Vector2.right);
        }

        private void Flip(int dir)
        {
            if (facingRight && dir == -1 || !facingRight && dir == 1)
            {
                facingRight = !facingRight;
                transform.Rotate(0f, 180f, 0f);
            }
        }

        protected void Jump()
        {
            if (grounded)
            {
                rb.AddForce(Vector2.up * jumpForce);
                grounded = false;
                doubleJumpReady = true;
            }
            else if (!grounded && doubleJumpReady)
            {
                rb.AddForce(Vector2.up * jumpForce);
                doubleJumpReady = false;
            }
        }       
    }
}


I don't know if it is because of my jump script, or my player script:

void Update()
{
    if (playerIdx == 1)
    {
        if (Input.GetKey(KeyCode.A))
            Move(-1);
        if (Input.GetKey(KeyCode.D))
            Move(1);
        if (Input.GetKey(KeyCode.W))
            Jump();
    }

    if (playerIdx == 2)
    {
        if (Input.GetKey(KeyCode.LeftArrow))
            Move(-1);
        if (Input.GetKey(KeyCode.RightArrow))
            Move(1);
        if (Input.GetKey(KeyCode.UpArrow))
            Jump();
    }
}

So how can I fix this?

Franz Wachter

as far as i can see you never reset the

doubleJumpReady = false;

Variable. To fix this simply change the jump code to:

protected void Jump()
{
    if (grounded)
    {
        rb.AddForce(Vector2.up * jumpForce);
        grounded = false;
        doubleJumpReady = true;
    }
    else if (!grounded && doubleJumpReady)
    {
        rb.AddForce(Vector2.up * jumpForce);
        doubleJumpReady = false;
    }
}

Hope it works ;).

EDIT: grounded is set by overlapping spheres. Therefore no need to set it here. Use this code and press your jump btn 2 times and see if the Debug.Log message shows up. Also, your player ID (idx is not needed.) As far as i can see your script is attached two to different objects. Therefore their variables are not shared anyways.

        protected void Jump()
        {
            if (grounded)
            {
                rb.AddForce(Vector2.up * jumpForce);
                doubleJumpReady = true;
            }
            else if (!grounded && doubleJumpReady)
            {
                rb.AddForce(Vector2.up * jumpForce);
                doubleJumpReady = false;
                Debug.Log("I am double jumping");

            }
        } 

And the final problem is, you do not execute one of your jumps you execute both at once. THis happens due to your execution.

Input.GetKey(KeyCode.UP)

instead use:

Input.GetKeyDown(KeyCode.Up);

GetKeyDown returns true when the button is pressed. GetKey returns true WHILE the button is pressed.

Hope it works now ;)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Unity 2d jumping script

C# in Unity 3D/2D: Am I required to use Classes for every script?

Unity 2d Movement script issue

2d sprite goes too fast with transform.translate in C# script (unity)

Unity 2d game movement script issue - unable to jump

Unity 2D: Decrease jump distance without compromising jump height

Unity - Newly created Layer for platforms not present in 'What Is Ground' field for Character Controller script 2D

Unity 2D - Jump Animation Reset

Unity2D how to limit player jump

How do I make a 2D Unity Sprite Jump?

Unity 2D jump from circle to circle always facing up

Unity 2D Why does my character jump different heights every time?

unity - jump with rigidbody bug

Getting the player jump correctly in Unity3D

How to jump smoothly in Unity3D

Unity 2D - Null Reference upon script activation

Unity3d - Jump in collision function

How do i make a cooldown for my jump in unity 2d? [found an answer]

CharacterController jump in unity

Unity 2D - Jump Function Doesn't Work Properly

Get Prefab in the script of an Instantiated Object - Unity 2D

Trouble with a Unity 2D movement script

How can I refactor my 2d unity movement script to resolve the behavioral issues I have?

Double Jump in unity 2D and code optimization

Create unity 2d animation via c# script

Cannot add my scoretext to script in gamecontrol in unity 2d

Unity 2d mobile button c# script is not working

Unable to jump in Unity2D

A game like Doodle Jump. I fly through the collider platforms instead of jumping on them. Unity 2D