Home > Blockchain >  Why is the IsGrounded function True when I press space, and not when im on the ground
Why is the IsGrounded function True when I press space, and not when im on the ground

Time:01-28

I'm new to Unity and I'm trying to make it so the player can't jump while they're already jumping using IsGrounded which should check if they're on the ground, but instead if press space then IsGrounded true. It might be an issue within Unity itself but im not sure. Thanks! The code:

using System.Collections.Generic;
using UnityEngine;


public class PlayerController : MonoBehaviour
{
    public float moveSpeed;

    float xInput, yInput;
    [SerializeField] private LayerMask lm;
    private Rigidbody2D rb;
    private BoxCollider2D bc;
    // Start is called before the first frame update
    void Start()
    {

    }
    private void Awake()
     {
      rb = transform.GetComponent<Rigidbody2D>();
      bc = transform.GetComponent<BoxCollider2D>();

    }



    // Update is called once per frame
    void Update()
    {

        if (IsGrounded() && Input.GetKeyDown(KeyCode.Space))
        {
            float jumpVelocity = 25f;
            rb.velocity = Vector2.up * jumpVelocity;
            Debug.Log("IsGrounded");
        } 
        xInput = Input.GetAxis("Horizontal");

        transform.Translate(xInput * moveSpeed, 0, 0);
        PlatformerMove();
        
    }

    void PlatformerMove()
    {
        rb.velocity = new Vector2(moveSpeed * xInput, rb.velocity.y);

    }

    private bool IsGrounded()
    {
        RaycastHit2D rc = Physics2D.BoxCast(bc.bounds.center, bc.bounds.size, 0f, Vector2.down * .1f, lm);
        return rc.collider != null;

    }
}```

CodePudding user response:

You are using a

Physics2D.BoxCast(bc.bounds.center, bc.bounds.size, 0f, Vector2.down * .1f, lm);

Now if you look at the parameters of Physics2D.BoxCast

public static RaycastHit2D BoxCast(
    Vector2 origin, Vector2 size, 
    float angle, Vector2 direction, 
    float distance = Mathf.Infinity, 
    int layerMask = Physics2D.AllLayers, 
    float minDepth = -Mathf.Infinity, 
    float maxDepth = Mathf.Infinity
); 

You are passing in

  • origin = bc.bounds.center
  • size = bc.bounds.size
  • direction = Vector2.down (the magnitude doesn't matter for a distance!)
  • distance = lm (I think you note the issue now) => I suppose this is a layermask -> some rather big integer -> you shoot very far -> probably always hit the something
  • layerMask = Physics2D.AllLayers

In general, since it is a Boxcast this means you are shooting a box into space and check if on its way it is hitting something.

If that's intended you would rather pass in

RaycastHit2D rc = Physics2D.BoxCast(bc.bounds.center, bc.bounds.size, 0f, Vector2.down, 0.1f, lm);

Otherwise I suppose you rather would want to use Physics2D.OverlapBox instead which rather checks for an overlap in a fixed position/area without shooting it anywhere

RaycastHit2D rc = Physics2D.OverlapBox(bc.bounds.center, bc.bounds.size, 0f, lm);
  • Related