I cant get my 2D Objects to collide

Hey! so im trying to get two of my Game Objects to collide and no matter what I do I cant figure out why they wont. They both have boxcolliders2D and rigidbody2D and are both on the same layer.

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

public class PlayerController : MonoBehaviour
{
    public float speed = 2;
    public float rotationSpeed = 10;
    public float verticalInput;
    public bool gameOver = false;
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void FixedUpdate()
    {
        if (transform.position.y > 4)
        {
            transform.position = new Vector3(transform.position.x, 4 , transform.position.z);
        }
        if (transform.position.y < -4)
        {
            transform.position = new Vector3(transform.position.x, -4 , transform.position.z);
        }
        //Get vertical Input from player
        verticalInput = Input.GetAxis("Vertical");

        //lets the player move
        transform.Translate(Vector3.up * Time.deltaTime * speed * verticalInput);
    }

    void onCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.CompareTag("Obstacle"))
        {
            Debug.Log("Hit");
        }

    }
}

And the object I want to move does have the “Obstacle” tag as well. Let me know if you need any more info to help out, I super appreciate it!

Don’t use transform to move, it doesn’t register collisions. You need to use the Rigidbody2D.velocity/Rigidbody2D.AddForce/Rigidbody2D.MovePosition to get collisions to happen.

1 Like

You are amazing my guy! Thank you so much cus this has been driving me crazy lol.