0
votes

For some reason, my player is not colliding with the tilemap walls that have collision on them... Here is my player movement code

 void Update()
    {
        hInput = Input.GetAxisRaw("Horizontal");
        vInput = Input.GetAxisRaw("Vertical");
        
        if (Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.RightArrow))
        {
            transform.Translate(hInput, 0, 0);
        }
        else if (Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.DownArrow))
        {
            transform.Translate(0, vInput, 0);
        }
        
        
    }

When I move my player they go right through the colliders. They are on the same Layer and both have colliders. Anyone know why this is happening? Thanks!

Edit: So I've tested the collisions with OnCollisionEnter2d like suggested and they are colliding, the problem is my player still walks right through the wall. I have no clue why the collider doesn't stop this from happening.

3
First thing I check is layers matrix and if there is a Rigidbody2D on one of the 2 colliding colliders. Or they may be set as triggers. - Nikaas

3 Answers

0
votes

The detection of colliders does not happen in Update() method. To detect collision you need to have OnCollisionEnter(). This method will be called when another object with collider and rigidbody enter in it. See the official example here.

void OnCollisionEnter(Collision collision)
{
    Debug.Log(collision.collider.name);
}
0
votes

Make sure the Z axis values are the same.

And just to check if the collisions are actually taking place, do this:

void OnCollisionEnter(Collision col)
{
    Debug.Log(col.collider.name);
}

And make sure that the Collider is not a Trigger.

These are the only possible problems, and their fixes.

0
votes

Collider doesn’t actually perform physics like the aftermath of a collision, it just figures out when things are touching. In order to use physics you need a rigidbody or rigidbody2D, which will actually do something about the collisions the collider detects. Then, instead of setting position you will need to set rb.velocity.