Friday, August 12, 2011

One kind of "3d" radar for Unity3D

3D Radar working in Unity3D
I wanted to take the radar script that is freely available on the Unity3D wiki page here and convert it from a traditional 2D representation to a 3d one - A radar with blips that float around your character.

As you can see in the above image, when you attach this script to a gameobject (and supply a prefab - in this case a basic sphere) the script will search for objects with a tag and color the spheres appropriately for the tag, moving them around the character to indicated the position of objects from the character. In the above case, blue dots are pointing to NPCs, and green dots to objects tagged "POI" (point of interest).

C# Script follows:



//
// Radar - Based off of the other radar scripts I've seen on the wiki
// but added 3d support for a 3d radar in the vein of the noise radar in metal gear solid 4
// only with easy prefabs instead of the full 'noise' idea. It wouldnt be hard to make the noise idea
// based off this script.
// By: Jason Lambert
// email: shotgunkiwi@gmail.com
//




using UnityEngine;
using System.Collections;


public class RealRadar : MonoBehaviour
{


    public enum RealRadarType : int { FullCircle }; //just one type for now


    // Display Location
    public Vector2 radarLocationCustom;
    public RealRadarType radarType = RealRadarType.FullCircle;
   
    public float radarSize = 0.20f;  // The amount of the screen the radar will use
    public float radarMaxDistance = 10.00f;
    public float yoffset = 1.0f;
    public int maxBlips = 100;
    public float minRadarDistance = 1.0f;
    public Transform blipPrefab = null;


    // Blip information
    public bool radarBlip1Active;
    public Color radarBlip1Color = new Color(0, 0, 255);
    public string radarBlip1Tag;


    public bool radarBlip2Active;
    public Color radarBlip2Color = new Color(0, 255, 0);
    public string radarBlip2Tag;


    public bool radarBlip3Active;
    public Color radarBlip3Color = new Color(255, 0, 0);
    public string radarBlip3Tag;


    public bool radarBlip4Active;
    public Color radarBlip4Color = new Color(255, 0, 255);
    public string radarBlip4Tag;


    // Internal vars
    private Vector2 _radarCenter;


    private GameObject _centerObject;


    private ArrayList blips = new ArrayList();






    // Initialize the radar
    void Start()
    {


        // Get our center object
        _centerObject = this.gameObject;


        if (blipPrefab != null)
        {
            for (int i = 0; i < maxBlips; i++)
            {
                Transform blip = (Transform.Instantiate(blipPrefab) as Transform);
                blip.localPosition = Vector3.zero;
                blip.parent = _centerObject.transform;
                blips.Add(blip);
                blip.gameObject.renderer.enabled = false;
            }
        }
    }


    // Update is called once per frame
    void Update()
    {
        GameObject[] gos;
        int blipNo = 0;


        


        // Position blips
        if (radarBlip1Active)
        {
            // Find all game objects
            gos = GameObject.FindGameObjectsWithTag(radarBlip1Tag);


            // Iterate through them and call drawBlip function
            foreach (GameObject go in gos)
            {
                updateBlip(go, radarBlip1Color, blips[blipNo++] as Transform);
            }
        }
        if (radarBlip2Active)
        {
            gos = GameObject.FindGameObjectsWithTag(radarBlip2Tag);


            foreach (GameObject go in gos)
            {
                updateBlip(go, radarBlip2Color, blips[blipNo++] as Transform);
            }
        }
        if (radarBlip3Active)
        {
            gos = GameObject.FindGameObjectsWithTag(radarBlip3Tag);


            foreach (GameObject go in gos)
            {
                updateBlip(go, radarBlip3Color, blips[blipNo++] as Transform);
            }
        }
        if (radarBlip4Active)
        {
            gos = GameObject.FindGameObjectsWithTag(radarBlip4Tag);


            foreach (GameObject go in gos)
            {
                updateBlip(go, radarBlip4Color, blips[blipNo++] as Transform);
            }
        }




        for (int i = blipNo; i < maxBlips; i++ )
        {
            (blips[i] as Transform).gameObject.renderer.enabled = false;
        }




    }


    // Draw a blip for an object
    void updateBlip(GameObject go, Color blipcolor, Transform blip)
    {
        if (_centerObject)
        {
            Vector3 centerPos = _centerObject.transform.position;
            Vector3 extPos = go.transform.position;


            // Get the distance to the object from the centerObject
            float dist = Vector3.Distance(centerPos, extPos);


            float scalef = dist / radarMaxDistance;
            //if (scalef < 1.0f) //uncomment this section if you want to use the maxdistance property
            //{
                blip.renderer.enabled = true;
                Vector3 unit = (extPos - centerPos).normalized;
                blip.position = centerPos + unit * (radarSize * scalef) + minRadarDistance * unit;
                blip.position = new Vector3(blip.position.x, _centerObject.transform.position.y + yoffset, blip.position.z);
                blip.renderer.material.SetColor("_Color", blipcolor);
            //} //uncomment this section if you want to use the maxdistance property
            //else 
            //{
            //    blip.renderer.enabled = false;
            //}//
            
        }
    }


   


}



No comments:

Post a Comment