Box collider 2d not working in vertical, but working on horizontal.

Hi all,

Basically i made two platforms, one of them in horizontal, and the other one is a wall (vertical). I made a boxcollider 2d and a rigidbody for the main player, and i made another boxcollider to the wall, however the collider doesn’t work unless its in horizontal.

Here you have a picture of what i mean.

8757940--1187029--upload_2023-1-26_10-28-50.png

As you can see, the main player (the white circle) isn’t collisioning, however the enemies are doing it well.

Any idea? please reply.

Thanks.

EDIT:

Here is the script of the player:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class player : MonoBehaviour
{
    private bool Jump = false;
    private int Lifes;
    public GameObject attack_original;
    public GameObject attack_position;

    // Start is called before the first frame update
    void Start()
    {
        Lifes = 10000;
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
        {
            transform.Translate(new Vector3(-0.005f, 0.0f));
        }
        if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
        {
            transform.Translate(new Vector3(0.005f, 0.0f));
        }
        if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow))
        {
           if (Jump == false)
            {
                    GetComponent<Rigidbody2D>().AddForce(new Vector2(0.0f, 300.0f));
                    Jump = true; 
            }
           /* GetComponent<Rigidbody2D>().AddForce(new Vector2(0.0f, 300.0f));
            Jump = true;*/
        }
        if (Input.GetKeyDown(KeyCode.Space))
        {
            GameObject.Instantiate(attack_original, attack_position.transform.position, attack_position.transform.rotation);

        }

     
    }
    void OnCollisionEnter2D(Collision2D _col)
    {
        switch (_col.gameObject.tag)
        {
            case "ground":
                Jump = false;
                break;
            case "wall":
                Jump = false;
                break;
            default:
                Jump = false;
                break;
        }
      
        /*if (_col.gameObject.tag == "ground")
        {
            Jump = false;
        }*/
        if (_col.gameObject.tag == "Enemy")
        {
            _col.gameObject.SetActive(false);
            Destroy(_col.gameObject, 0.05f);
            Lifes = Lifes - 1;
            if (Lifes <= 0)
            {
                Debug.Log("GAME OVER!");
                gameObject.SetActive(false);
            }
        }
    }
}

Possible causes:

  • Your player object may not have a Rigidbody2D component.

  • The Rigidbody2D type can be a different type, if something is dynamic then it doesn’t work with the kinematic type.

  • You don’t have Collider2D on your player.

  • Collider2D is wrongly assigned or has an isTrigger mode

If you have the Translate method, use Vector3.left (or right) * Time.deltaTime * speed (its like basic setting for such things). It’s possible that using a regular Vector * frame rate gives you too much speed. This breaks the rules of Physics2D and pierces the object.

As Mr. Angry said, it is probably because you are manipulating the transform via Translate on an object with a rigidbody which uses physics.
8758510--1187152--upload_2023-1-26_8-11-2.png

Either avoid physics entirely or avoid using transform.Translate and move your character via physics like MovePosition or AddForce. I would suggest avoid using transform.Translate since you probably want some sort of physics in the rest of your level/game.

Here is a better explanation Differences between transform.Translate, rigidbody.Velocity and rigidbody.MovePosition? - Unity Forum