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: http://blog.growthlikes.com/2016/09/invisible-boundaries-walls-script-in.html
 *
 * Original code by: BrettFromLA (http://forum.unity3d.com/threads/invisible-boundaries-walls.45044/)
 * Updated and translated to C# by: Nauta Dev (https://nautadev.blogspot.com)
 *
 */

using UnityEngine;

public class InvisibleFence : MonoBehaviour {

    //PUBLIC INSPECTOR VARIABLES
    [SerializeField]
    public GameObject go_next_fence_post;
 
 
    //PRIVATE VARIABLES
 
    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;
 
 
    //MONOBEHAVIOUR FUNCTIONS
 
    void Awake()
    {
        //hide this fence post
        Destroy(GetComponent<MeshRenderer>());

        //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)
            {
                gameObject.AddComponent<MeshFilter>();
            }
            Mesh ms_mesh = GetComponent<MeshFilter>().mesh;

            //create the 4 vertices
            ms_mesh.Clear();
            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
            Destroy(GetComponent<BoxCollider>());
            Destroy(GetComponent<SphereCollider>());
            Destroy(GetComponent<CapsuleCollider>());
            Destroy(GetComponent<MeshCollider>());
            Destroy(GetComponent<WheelCollider>());

            //create a mesh collider in this shape
            gameObject.AddComponent<MeshCollider>();
            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 (https://twitter.com/nautadev) and in my new YouTube channel!
Invisible Boundaries / Walls Script in Unity Reviewed by A Work Online on 10:34:00 PM Rating: 5

1 comment:

All Rights Reserved by Ondade © 2017

Contact Form

Name

Email *

Message *

Powered by Blogger.