How to create a minimap for VR in Unity

ToledoVR – Mini Map

1. Create the minimap model from Terrain

Use the following Script to create a Model from the Terrain:

http://wiki.unity3d.com/index.php?title=TerrainObjExporter

2. Add the Model to your Scene

3. Create the Texture for your Model

Use a Orthographic Camera to take a picture of your Terrain.

Use print screen to save the image.

4. Create the Material

Create a new Material and add the picture in step 3. Add this material to all the Meshes of your Minimap.

5. Add the Script

Adding this script and setting the appropriate values in the Inspector will make an Object on the minimap mirror the movment of the corresponding Object.

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

public class MiniMap : MonoBehaviour
{

    public Transform trackedObject;
    public Transform miniMapObject;
    public Transform baseOrig;
    public Transform boundOrigX;
    public Transform boundOrigZ;
    public Transform boundMiniX;
    public Transform boundMiniZ;
    public float lengthXOriginal;
    public float lengthYOriginal;
    public float lengthZOriginal;
    public float lengthXMiniMap;
    public float lengthYMiniMap;
    public float lengthZMiniMap;

    private float ratioX;
    private float ratioY;
    private float ratioZ;

    public GameObject dynamicObject;

    void Start()
    {
        lengthXOriginal = Vector3.Distance(baseOrig.position, boundOrigX.position);
        lengthZOriginal = Vector3.Distance(baseOrig.position, boundOrigZ.position);
        lengthXMiniMap = Vector3.Distance(transform.position, boundMiniX.position);
        lengthZMiniMap = Vector3.Distance(transform.position, boundMiniZ.position);
        ratioX = (lengthXMiniMap / lengthXOriginal);
        ratioY = (lengthYMiniMap / lengthYOriginal);
        ratioZ = (lengthZMiniMap / lengthZOriginal);
    }

    // Update is called once per frame
    void Update()
    {
        Vector3 positionGod = trackedObject.position - baseOrig.position;
        Vector3 newPositionGod = new Vector3();
        newPositionGod.x = ratioX * positionGod.x;
        newPositionGod.y = ratioY * positionGod.y;
        newPositionGod.z = ratioZ * positionGod.z;
        miniMapObject.localPosition = newPositionGod;
    }

    public void AddDynamicObject(Transform transform)
    {
        GameObject go = Instantiate(dynamicObject, this.transform);
        MiniMapEnemy miniMapEnemy = go.GetComponent<MiniMapEnemy>();
        miniMapEnemy.Initialize(baseOrig,transform,ratioX,ratioY,ratioZ);

    }

}

Add this script to objects that are created dynamicly.

 

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

public class MiniMapEnemy : MonoBehaviour
{
    private Transform myTransform;

    public Transform pivot;
    public Transform master;
    public float ratioX;
    public float ratioY;
    public float ratioZ;
    private bool inizialized;

    void Start()
    {
        myTransform = transform;
    }

    public void Initialize(Transform pivot, Transform master, float ratioX, float ratioY, float ratioZ)
    {
        this.pivot = pivot;
        this.master = master;
        this.ratioX = ratioX;
        this.ratioY = ratioY;
        this.ratioZ = ratioZ;
        inizialized = true;
    }

    // Update is called once per frame
    void Update()
    {
        if (!inizialized)
        {
            return;
        }
        if (master != null)
        {
            Vector3 position = master.position - pivot.position;
            Vector3 newPosition = new Vector3();
            newPosition.x = ratioX * position.x;
            newPosition.y = ratioY * position.y;
            newPosition.z = ratioZ * position.z;
            myTransform.localPosition = newPosition;
        }
        else
        {
            Destroy(gameObject);
        }
    }
}

 

6. Make the Minimap Interactable

Use VRTK’s Interctable_Object to make your minimap grabbable.

Leave a Reply

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