Player should be able to go through Wall


i am doing a game in which a player should be able to walk through walls. Walls are 3D cubes objects in my game. I can’t just uncheck the “box collider” of the wall prefab, because I also have players who are monsters and who shall NOT be able to walk through walls, only human player can.I have tried different approaches, e.g. tagging the player and attaching a script to the wall which uses "if (collision.gameObject.tag == ""Player) {Physics.IgnoreCollision(…)}. I tried different layers for walls and players using “Physics.IgnoreLayerCollision…()…”. But Nothing works. What am I missing?


Edit → Project Settings → Physics

The Layer Collision Matrix defines how collisions on different layers will react.

Read more: Unity Manual - Layer Based Collision


OK< well here is something that depends also on what version of Unity you are using. For sake of argument I am going to assume the most recent.

Lets say your movement code looks like this;

 transform.Rotate(0, Input.GetAxis("Horizontal") * rotateSpeed, 0);
            Vector3 forward = transform.TransformDirection(Vector3.forward);
            float curSpeed = speed * Input.GetAxis("Vertical");

it will detect any mesh you have in scene as a player.

If you have something like this

Input.GetAxis("Horizontal") * Time.deltaTime, 0f, moveSpeed * Input.GetAxis("Vertical") * Time.deltaTime);

it passes through most objects even with mesh colliders applied.

The other factor depends on if you are using a capsule collider on the person or a Character Controller collider. In most cases, simply checking them to trigger will allow you character to pass through objects, but leaving the colliders on enemies as unchecked will keep them from passing through said mesh collider set objects you placed.

So that’s really the only two parts you need to check, the type of movement script and the collider type and its settings.

Hope that helps.

A game object can only be in one layer. The layer needs to be in the range [0…31]
Chapter1.WalkThroughWall:SetLayerToNotSolid() (at Assets/My script/Chapter1/WalkThroughWall.cs:25)
Chapter1.GameManager_EventMaster:CallMyGeneralEvent() (at Assets/My script/Chapter1/GameManager_EventMaster.cs:18)
Chapter1.TriggerExample:OnTriggerEnter(Collider) (at Assets/My script/Chapter1/TriggerExample.cs:22)