Forum begins after the advertisement:


[Part 1] My player isn’t jumping no matter what I do.

Home Forums Video Game Tutorial Series Creating a Metroidvania in Unity [Part 1] My player isn’t jumping no matter what I do.

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #12327
    demilade
    Participant
    Helpful?
    Up
    0
    ::

    I have no idea why the player doesn’t jump. Here is the code.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class PlayerController : MonoBehaviour
    {
        [Header("Horizontal Movement Settings:")]
        [SerializeField] private float walkSpeed = 1;
    
        [SerializeField] private float jumpForce = 45;
    
        [Header("Ground Check Settings:")]
        [SerializeField] private Transform groundCheckPoint;
        [SerializeField] private float groundCheckY = 0.2f;
        [SerializeField] private float groundCheckX = 0.5f;
        [SerializeField] private LayerMask whatIsGround;
    
        //References
        Rigidbody2D rb;
        private float xAxis;
    
        void Start()
        {
            rb = GetComponent<Rigidbody2D>();
        }
    
        void Update()
        {
            GetInputs();
            Move();
            Jump();
        }
    
        void GetInputs()
        {
            xAxis = Input.GetAxisRaw("Horizontal");
        }
    
        void Move()
        {
            rb.velocity = new Vector2(walkSpeed * xAxis, rb.velocity.y);
        }
    
        public bool Grounded()
        {
            if (Physics2D.Raycast(groundCheckPoint.position, Vector2.down, groundCheckY, whatIsGround)
                || Physics2D.Raycast(groundCheckPoint.position + new Vector3(groundCheckX, 0, 0), Vector2.down, groundCheckY, whatIsGround)
                || Physics2D.Raycast(groundCheckPoint.position + new Vector3(-groundCheckX, 0, 0), Vector2.down, groundCheckY, whatIsGround))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    
        void Jump()
        {
            if (Input.GetButtonUp("Jump") && rb.velocity.y > 0)
            {
                rb.velocity = new Vector2(rb.velocity.x, 0);
            }
    
            if (Input.GetButtonDown("Jump") && Grounded())
            {
                rb.velocity = new Vector3(rb.velocity.x, jumpForce);
            }
        }
    }

    Can you guys please help me with this?

    #12329
    Terence
    Keymaster
    Helpful?
    Up
    0
    ::

    Hi demilade, you will only be able to jump when the Grounded() function returns true. That means that this section here needs to be true:

    public bool Grounded()
    {
        if (Physics2D.Raycast(groundCheckPoint.position, Vector2.down, groundCheckY, whatIsGround)
            || Physics2D.Raycast(groundCheckPoint.position + new Vector3(groundCheckX, 0, 0), Vector2.down, groundCheckY, whatIsGround)
            || Physics2D.Raycast(groundCheckPoint.position + new Vector3(-groundCheckX, 0, 0), Vector2.down, groundCheckY, whatIsGround))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    You need to make sure that your Ground Check X and Ground Check Y have non-zero values, your Ground Check Point is set, and the What Is Ground property is set to the correct layers.

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: