Tutorial on How to make the player or Enemy go through the walls in Unity

Let’s start with the Enemy going through the walls. The enemy for Example could be a Ghost.

To make the Ghost Enemy Object follow our player we could use the script used in this tutorial:

How to make Enemy follow the player in Unity Tutorial

As it turns out, we don’t have to do anything, because our script enables already the enemy to go through the walls.

But what about our player?

As it turns out we don’t have such powers yet. We are colliding with the wall object and can’t go through them.

But what if… we could create a Pill which would enable us to go through the walls? The Pill would have a Green color and it would enable us to go through the walls for 5 seconds.

If we collide with it, the object would be destroyed.

To make it happen we could write a simple script:

using UnityEngine;

public class WalkThroughWalls : MonoBehaviour {

    void OnCollisionEnter(Collision col)
    {
        if (col.gameObject.name == "GreenPill")
        {
            Destroy(col.gameObject);
        }
    }
}

At this moment the object will be destroyed, but we can’t go through the walls. Not yet.

Don’t forget to name your “Pill” Object “GreenPill”. Otherwise it won’t work!

We need also to modify the Script. At the end we get something like this:

using System.Collections;
using UnityEngine;

public class WalkThroughWalls : MonoBehaviour {

    void OnCollisionEnter(Collision col)
    {
        if (col.gameObject.name == "GreenPill")
        {

            Destroy(col.gameObject);

            GameObject[] gos;
            gos = GameObject.FindGameObjectsWithTag("Wall");

            foreach (GameObject go in gos)
            {
                go.GetComponent<BoxCollider>().enabled = false;

                StartCoroutine(DisableBox(5.0F));

            }
        }
    }

    IEnumerator DisableBox(float waitTime)
    {
        yield return new WaitForSeconds(waitTime);
        GameObject[] gos;
        gos = GameObject.FindGameObjectsWithTag("Wall");

        foreach (GameObject go in gos)
        {

            go.GetComponent<BoxCollider>().enabled = true;
        }

    }
}

What did we do in our script?

We put all of the objects with the tag “Wall” in an array and disabled all of their Box Colliders for 5 seconds.

Why only 5 seconds? Well it doesn’t need to be exactly 5 seconds. It could be 2 or 10. It all depends from the Developer, which value is he going to set in this Corutine:

StartCoroutine(DisableBox(5.0F));

We called IEnumerator function with a Coroutine

This way, after 5 seconds are over we can’t go through Walls anymore.

That’s all for Today. I hope you learned something and that i helped you with your project!

Other than that, check other tutorials in the Unity series:

Spawning elements in Unity on Button Click

Making the camera follow an Object in Unity3D

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.