Hitbox problem (New on Unity!)

Hello you all!

I recently started programming in Unity, since I always worked on GameMaker, I think it’s a good decision to also learn Unity, as it’s more famous than GM and basically almost all the companies uses it.

Nevermind, I started my project and I am working on my player’s hitbox and collisions with Tilemaps. It works very well for jumping and running, but there’s a little problem when I am in front of a wall and I try to jump it, let me show to you:

on8nmm

As you can see, I can move and jump freely, but if I jump just next to a wall, my player clips there, and then I can’t jump again, so I think something is not good here. I suppose is some problem with the Collider 2D? I had the same issue in GM and the problem was the Collision Mask, but I don’t what is causing this in Unity.

In case you need more info, here’s my player movement code:

public int velocity= 7;
    public int jump= 100;

    private float movement;
    private int scale = 1;
    private bool canJump;

    private Rigidbody2D rb;
    private Animator anim;

    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        anim = GetComponent<Animator>();
    }

    void Update()
    {

        //Jump
        if (Input.GetKeyDown(KeyCode.Z) && canJump)
        {
            rb.AddForce(Vector2.up * jump, ForceMode2D.Impulse);
            canJump = false;
        }

        //Check for movement
        if (Input.GetKey("right"))
        {
            movement = 1;
        }
        else if (Input.GetKey("left"))
        {
            movement = -1;
        }
        else movement = 0;
     

        //Running animation
        anim.SetBool("running", movement != 0);

        //Scaling in direction
        if (movement != 0)
        {
            scale = movement < 0 ? -1 : 1;
        }
        transform.localScale = new Vector3(scale, 1, 1);



    }

    private void FixedUpdate()
    {
        rb.velocity = new Vector2(movement * velocity, rb.velocity.y);
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if(collision.transform.tag == "floor")
        {
            canJump = true;
        }
    }

Appreciate the help!
Thanks.

This isn’t your problem, but you can replace lines 28-36 with:

movement = Input.GetAxisRaw("Horizontal");

And this will also be more versatile in terms of player input (allowing gamepad control, etc).

Your problem probably has to do with the friction/physics settings on the ground/tilemap collider, if I had to guess.

1 Like

There are a few things you may want to try to resolve this issue.
First, you can set your player’s Rigidbody2D Collision Detection to “Continuous”. This helps prevent situations where the player clips slightly into other colliders.
9806037--1408023--upload_2024-4-30_13-42-8.png

Second, you may need to create PhysicsMaterial2D with Friction set to 0 so that the wall’s friction doesn’t prevent you from falling. You can apply this to the RigidBody2D as well.

This solved the problem! I already had the Collision Detection as “Continuous”, but the PhysicsMaterial2D solved the problem. Thank you!