Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Hitbox problem (New on Unity!)

Discussion in 'Getting Started' started by Jamope_, Apr 30, 2024.

  1. Jamope_

    Jamope_

    Joined:
    Apr 29, 2023
    Posts:
    6
    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:



    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:

    Code (CSharp):
    1. public int velocity= 7;
    2.     public int jump= 100;
    3.  
    4.     private float movement;
    5.     private int scale = 1;
    6.     private bool canJump;
    7.  
    8.     private Rigidbody2D rb;
    9.     private Animator anim;
    10.  
    11.     void Start()
    12.     {
    13.         rb = GetComponent<Rigidbody2D>();
    14.         anim = GetComponent<Animator>();
    15.     }
    16.  
    17.     void Update()
    18.     {
    19.  
    20.         //Jump
    21.         if (Input.GetKeyDown(KeyCode.Z) && canJump)
    22.         {
    23.             rb.AddForce(Vector2.up * jump, ForceMode2D.Impulse);
    24.             canJump = false;
    25.         }
    26.  
    27.         //Check for movement
    28.         if (Input.GetKey("right"))
    29.         {
    30.             movement = 1;
    31.         }
    32.         else if (Input.GetKey("left"))
    33.         {
    34.             movement = -1;
    35.         }
    36.         else movement = 0;
    37.      
    38.  
    39.         //Running animation
    40.         anim.SetBool("running", movement != 0);
    41.  
    42.         //Scaling in direction
    43.         if (movement != 0)
    44.         {
    45.             scale = movement < 0 ? -1 : 1;
    46.         }
    47.         transform.localScale = new Vector3(scale, 1, 1);
    48.  
    49.  
    50.  
    51.     }
    52.  
    53.     private void FixedUpdate()
    54.     {
    55.         rb.velocity = new Vector2(movement * velocity, rb.velocity.y);
    56.     }
    57.  
    58.     private void OnCollisionEnter2D(Collision2D collision)
    59.     {
    60.         if(collision.transform.tag == "floor")
    61.         {
    62.             canJump = true;
    63.         }
    64.     }
    Appreciate the help!
    Thanks.
     
    Last edited: Apr 30, 2024
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,777
    This isn't your problem, but you can replace lines 28-36 with:
    Code (csharp):
    1. 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.
     
    Jamope_ likes this.
  3. dstears

    dstears

    Joined:
    Sep 6, 2021
    Posts:
    150
    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.
    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.
     
  4. Jamope_

    Jamope_

    Joined:
    Apr 29, 2023
    Posts:
    6
    This solved the problem! I already had the Collision Detection as "Continuous", but the PhysicsMaterial2D solved the problem. Thank you!