user6853421
user6853421

Reputation:

Unity 2D - Jump Function Doesn't Work Properly

I'm new to coding and to Unity2D, so please bear with me.

I've created a Character Controller, which is called via EventTriggers set against UI button objects. The controller works fine (though is probably overkill), but below are the challenges around the jump function I'm having. Any help on this is really appreciated.

Thanks so much!

Challenges

Here is the script:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class GameController : MonoBehaviour
    {
        Rigidbody2D rb2d;
        SpriteRenderer spriteRenderer;
        Animator animator;
        public Transform groundCheck;
        private LayerMask groundLayer = 1 << LayerMask.NameToLayer ("groundLayer");
        private bool moveLeft;
        private bool moveRight;
        private bool moveJump;
        private bool isGrounded;
        private float horizontalMove;
        private float verticalMove;
        public float playerSpeed = 0f;
        public float jumpForce = 0f;
        public float groundCheckRadius;
    
        public void Start()
        {
            rb2d = GetComponent<Rigidbody2D>();
            spriteRenderer = GetComponent<SpriteRenderer>();
            animator = GetComponent<Animator>();
    
            moveLeft = false;
            moveRight = false;
            moveJump = false;
        }
    
        //Function - Left button pressed
        public void MoveLeft()
        {
            moveLeft = true;
            Debug.Log("Function - Move Left");
        }
    
        //Function - Right button pressed
        public void MoveRight()
        {
            moveRight = true;
            Debug.Log("Function - Move Right");
        }
    
        //Function - Jump button pressed
        public void MoveJump()
        {
            moveJump = true;
            Debug.Log("Function - Move Jump");
        }
    
        //Function - Stop player movement
        public void MoveStop()
        {
            rb2d.velocity = Vector3.zero;
            animator.Play("PlayerIdle");
            moveLeft = false;
            moveRight = false;
            moveJump = false;
        }
    
        //Function - Check if player 'GroundCheck' is touching the ground layer
        private void PlayerGrounded()
        {
            isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, groundLayer);
    
            if (isGrounded == true)
            {
                Debug.Log("Player is touching the ground!");
            }
    
            if (isGrounded == false)
            {
                Debug.Log("Player is not touching the ground!");
            }
    
        }
    
        //Function - Plays once every frame
        private void Update()
        {
            PlayerMovement();
    
            PlayerGrounded();
        }
    
        //Function - Move the player GameObject
        public void PlayerMovement()
        {
    
            //Set the player horizontal right axis
            if (moveRight)
            {
                horizontalMove = playerSpeed;
                Debug.Log("Function - PlayerMovement - Move Right!");
            }
    
            //Set the player horizontal left axis
            if (moveLeft)
            {
                horizontalMove = -playerSpeed;
                Debug.Log("Function - PlayerMovement - Move Left!");
            }
    
            //Set the player vertical axis
            if (moveJump)
            {
                verticalMove = jumpForce;
                Debug.Log("Function - PlayerMovement - Move Jump!");
            }
    
        }
    
        //Function - Plays once every loaded frame
        private void FixedUpdate()
        {
    
            //Move the player right, don't flip, play animation
            if (moveRight)
            {
                rb2d.velocity = new Vector2(horizontalMove, rb2d.velocity.y * playerSpeed * Time.deltaTime);
                spriteRenderer.flipX = false;
                animator.Play("PlayerRun");
                Debug.Log("Function - Player Moving Right!");
            }
    
            //Move the player left, don't flip, play animation
            if (moveLeft)
            {
                rb2d.velocity = new Vector2(horizontalMove, rb2d.velocity.y * playerSpeed * Time.deltaTime);
                spriteRenderer.flipX = true;
                animator.Play("PlayerRun");
                Debug.Log("Function - Player Moving Left!");
            }
    
            //Move the player into the air via a jump
            if (moveJump && (rb2d.velocity.y == 0))
            {
                rb2d.velocity = new Vector2(verticalMove, rb2d.velocity.x * jumpForce);
                animator.Play("PlayerJump");
                Debug.Log("Function - Player Moving Up!");
            }
    
        }
    
    }

Upvotes: 0

Views: 165

Answers (1)

gbe
gbe

Reputation: 1007

The problems are in the end of fixed update. This is what you have:

if (moveJump && (rb2d.velocity.y == 0))
{
    rb2d.velocity = new Vector2(verticalMove, rb2d.velocity.x * jumpForce);
    animator.Play("PlayerJump");
    Debug.Log("Function - Player Moving Up!");
}

Let’s look at the start. It says if (moveJump && (rb2d.velocity.y == 0). So, (I assume you are setting moveJump when they hit the jump button) if you hit the jump button and have a y velocity of 0. However, this does not have to do with if the player is on the ground. We should change it to if (moveJump && isGrounded). This way, it checks if isGrounded is true, before jumping.

When you change the velocity to jump, I notice a couple problems. This is what it says:

rb2d.velocity = new Vector2(verticalMove, rb2d.velocity.x * jumpForce);

When creating a new Vector2, the first argument is x, and the second is y. You are doing it in the opposite order. You did y, then x. You should swap them like this:

rb2d.velocity = new Vector2(rb2d.velocity.x, verticalMove * jumpForce);

You were applying the jump to the x axis, and the movement to the y axis because of this swap, but now it is fixed.

This is all of the changes together:

if (moveJump && (rb2d.velocity.y == 0))
{
    rb2d.velocity = new Vector2(verticalMove, rb2d.velocity.x * jumpForce);
    animator.Play("PlayerJump");
    Debug.Log("Function - Player Moving Up!");
}

Upvotes: 1

Related Questions