Top Ad unit 728 × 90

Invisible Boundaries / Walls Script in Unity

Few days ago, meanwhile I was developing a Unity game, I was looking for what was the best way to make invisible boundaries in Unity.

And I found a user in the Unity forums who made a very interesting script. But the bad thing was that it was in JS (I prefer to work in C#) and the worst thing: it was outdated.

Note: Don't forget to read these tips that every Unity developer should know (and use)

So, today I want to share with you the script in C# and updated to the last Unity version (5.4) at this time:

 * Base script was created by an user called BrettFromLA from Unity forums.
 * I have updated the code to the last Unity version (5.4) and translated the code
 * from JScript to C#.
 * You can see the way of use this code on my post:
 * Original code by: BrettFromLA (
 * Updated and translated to C# by: Nauta Dev (

using UnityEngine;

public class InvisibleFence : MonoBehaviour {

    public GameObject go_next_fence_post;
    private Vector3 v3_distance;
    private Vector3 v3_nextend1;
    private Vector3 v3_nextend2;
    private Vector3 v3_nexthalflength;
    private Vector3 v3_thisend1;
    private Vector3 v3_thisend2;
    private Vector3 v3_thishalflength;
    void Awake()
        //hide this fence post

        //is there another fence post?
        if (go_next_fence_post != null)

            //get Vector3's that are half the length of the fence pole heights, with the same rotations
            v3_thishalflength = transform.TransformDirection(Vector3.up * transform.localScale.y / 2);
            v3_nexthalflength = go_next_fence_post.transform.TransformDirection(Vector3.up * go_next_fence_post.transform.localScale.y / 2);

            //get the Vector3 that's the distance  direction between these two fence posts
            v3_distance = go_next_fence_post.transform.position - transform.position;

    void Start()
        //connect it to another fence post?
        if (go_next_fence_post != null)
            //normalize the scale of this object and set its rotation to nothin'
            transform.localScale = new Vector3(1, 1, 1);
            transform.rotation = Quaternion.Euler(new Vector3(0, 0, 0));

            //create the mesh filter
            if (GetComponent<MeshFilter>() == null)
            Mesh ms_mesh = GetComponent<MeshFilter>().mesh;

            //create the 4 vertices
            ms_mesh.vertices = new Vector3[] { v3_thishalflength, -v3_thishalflength, v3_distance + v3_nexthalflength, v3_distance - v3_nexthalflength };

            //create 4 triangles between the 2 fence posts, to cover the space between them thoroughly.  Create each triangle in 2 ways (like 0,1,2 and 0,2,1) so they're impenetrable from both sides
            ms_mesh.triangles = new int[] { 0, 1, 2, 0, 2, 1, 0, 1, 3, 1, 3, 1, 0, 2, 3, 0, 3, 2, 1, 2, 3, 1, 3, 2 };

            //remove all colliders

            //create a mesh collider in this shape
            GetComponent<MeshCollider>().sharedMesh = ms_mesh;

            //make sure these doen't interfere with OnMouseOver
            gameObject.layer = LayerMask.NameToLayer("Ignore Raycast");



How to use?

> Create a Cube and scale it to something like (0.01, 200, 0.01)
> Add this script to the Cube GameObject
> Place that cube where you want a boundary
> Duplicate the GameObject and place the new Cube in the next Boundary
> At the first Cube, in the script, in the var called go_next_fence_post drag the GameObject of the second Cube (because you want to connect the first boundary to the second)
> Repeat this to create boundaries in your scene

Do you have any question or improvement? Please, feel free to comment here! And remember to follow me in Twitter ( and in my new YouTube channel!
Invisible Boundaries / Walls Script in Unity Reviewed by Ondade Dev Academy on 10:34:00 PM Rating: 5

No comments:

All Rights Reserved by Ondade © 2017

Contact Form


Email *

Message *

Powered by Blogger.