Forum begins after the advertisement:


[Part 1] Groundcheck not working

Home Forums Video Game Tutorial Series Creating a Metroidvania in Unity [Part 1] Groundcheck not working

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #19699
    Luca Vestena
    Level 1
    Participant
    Helpful?
    Up
    0
    ::

    for some reason my charachter always thinks he is on the ground and can jump infinetly, i followed the tutorial exactly but it doesnt work, here is the code:

    using UnityEngine;
    
    public class PlayerController : MonoBehaviour
    {
        [Header("Horizontal Movement Settings")]
        private Rigidbody2D rb;
        [SerializeField] private float walkspeed = 1;
        private float xAxis;
    
        [Header("Ground Check Settings")]
        [SerializeField] private float jumpForce = 45;
        [SerializeField] private Transform GroundCheckpoint;
        [SerializeField] private float groundCheckY = 0.2f;
        [SerializeField] private float groundCheckX = 0.5f;
        [SerializeField] private LayerMask whatIsGround;
    
        // Start is called once before the first execution of Update after the MonoBehaviour is created
        void Start()
        {
            rb = GetComponent<Rigidbody2D>();
        }
    
        // Update is called once per frame
        void Update()
        {
            getinputs();
            Move();
            Jump();
    
            if (Input.GetButtonDown("Jump"))
            {
                rb.linearVelocity = new Vector2(rb.linearVelocity.x, jumpForce);
            }
        }
    
        void getinputs()
        {
            xAxis = Input.GetAxisRaw("Horizontal");
        }
    
        private void Move()
        {
            rb.linearVelocity = new Vector2(walkspeed * xAxis, rb.linearVelocity.y);
        }
    
        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.linearVelocityY > 0)
            {
                rb.linearVelocity = new Vector2(rb.linearVelocityX, 0);
            }
    
            if(Input.GetButtonDown("Jump") && Grounded())
            {
                rb.linearVelocity = new Vector3(rb.linearVelocity.x, jumpForce);
            }
        }
    }
    #19713
    Terence
    Level 32
    Keymaster
    Helpful?
    Up
    0
    ::

    Hi Luca, sorry for missing your message. Can you check whether your What Is Ground is set properly, and that all your ground tiles are in the one of the layers set in What Is Ground?

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: