No matter what I try, the wall collision just doesn’t work

No matter what I try in my game, wall collisions just don’t work.

I’ve tried doing it through code, 2D and 3D collisions, different types of colliders, and just no matter what I try, the wall collision just doesn’t work. The other collisions in the game work completely fine.

What am I doing wrong here?

I have attached the script I used to try to get it to work since it wouldn’t work on it’s own.

void OnCollisionEnter(Collision col)
{
    if(col.gameObject.layer == 9)
    {
        rb.constraints = RigidbodyConstraints.FreezePositionX;
    }
}

Here is the Rigidbody and Box Collider for the player (Is Trigger is set because it is affected by the player’s movement script):
PlayerRigidbody
And just in case, here’s the player movement code (the commented thing was just me messing around trying to get it to work):

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

public class AvatarMovement : MonoBehaviour
{
    public Rigidbody rb;
    public float yVelocity;
    public GameObject ground;
    public GameOver gameOver;
    public Renderer rend;
    public Material[] avatars;

    void Start()
    {
        rb = GetComponent<Rigidbody>();
        int avatar = PlayerPrefs.GetInt("ChosenAvatar");
        rend.material = avatars[avatar];
    }

    // Update is called once per frame
    void Update()
    {
        transform.position = new Vector3(transform.position.x, transform.position.y + yVelocity, transform.position.z);
        if (ground == null)
        {
            yVelocity -= 0.00002f;
        }
        if (Input.GetKeyDown("space") && ground != null && gameOver.playerAlive)
        {
            yVelocity = 0.011f;
            //    transform.position = new Vector3(transform.position.x, transform.position.y + 1.5f, transform.position.z);
        }
        if (Input.GetKey("left")||Input.GetKey("a"))
        {
            transform.position = new Vector3(transform.position.x - 0.005f, transform.position.y, transform.position.z);
        }

        if(Input.GetKey("right")||Input.GetKey("d"))
        {
            transform.position = new Vector3(transform.position.x + 0.005f, transform.position.y, transform.position.z);
        }

    }
    void OnTriggerEnter(Collider col)
    {
        float topColliderPoint = col.gameObject.transform.position.y + col.gameObject.transform.localScale.y / 2;
        float bottomPlayerPoint = transform.position.y - transform.localScale.y / 2;
        if (col.gameObject.layer == 3 && Mathf.Abs(topColliderPoint - bottomPlayerPoint) < 0.2)
        {
            yVelocity = 0;
            ground = col.gameObject;
        }
    }

    //void OnCollisionEnter(Collision col)
    //{
    //
      //  if (col.gameObject.layer == 7)
        //{
          //  if (Input.GetKeyDown("left"))
            //{
              //  rb.constraints = RigidbodyConstraints.FreezePositionX;
            //}
        //}
        //if (col.gameObject.layer == 8)
        //{
          //  if (Input.GetKeyDown("right"))
            //{
              //  GetComponent<Rigidbody>().velocity = Vector3.zero;
            //}
       // }
    //}

    void OnTriggerExit(Collider col)
    {
        if (ground == col.gameObject)
        {
            ground = null;
        }
    }
}

I will post the other info in a seperate comment because it won’t let me include it in one

Here’s the collider for the wall:
WallCollider

Here’s the Physics2D:
Physics

Hello to whoever’s reading this - I was able to figure out a solution by not using walls and just using coordinates, here is the code in case you’re curious:

if (Input.GetKey("left")||Input.GetKey("a"))
        {
            leftKeyPressed = true;
        }
        else
        {
            leftKeyPressed = false;
        }

        if(leftKeyPressed && player.transform.position.x > leftBound)
        {
            transform.position = new Vector3(transform.position.x - 0.005f, transform.position.y, transform.position.z);
        }

        if (Input.GetKey("right")||Input.GetKey("d"))
        {
            rightKeyPressed = true;
        }
        else
        {
            rightKeyPressed = false;
        }

        if (rightKeyPressed && player.transform.position.x < rightBound)
        {
            transform.position = new Vector3(transform.position.x + 0.005f, transform.position.y, transform.position.z);
        }

Thank you for the (brief) help.

Moving a dynamic RigidBody directly via its transform will bypass the physics engine and it may not receive any physics messages or forces because it’s been teleported.

You also cannot use OnCollisionEnter with any body marked as a Trigger because it needs to match up in the collision/trigger action matrices. You would need to use OnTriggerEnter.

You either have to:

  • Use forces to move your Rigidbody, so it interacts correctly with the physics engine.
  • Mark the Rigidbody as kinematic, which changes its properties so it’s intended to be moved manually. (Kinematic has more restrictions and less capabilities)
  • Manually check for collisions/overlaps.