Created
January 31, 2016 11:54
-
-
Save wtekteam/c9104d9c5c9d3a3ff669 to your computer and use it in GitHub Desktop.
Unity's radar effect: renders object which are just detected and then disable the renderer (out of the radar field of view).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Author: Fabien ROUALDES | |
// Date: 31/01/2016 | |
// | |
// | |
// Render objects which are in the radar field of view. | |
// The radar is rotating around a camera | |
// | |
using UnityEngine; | |
using System.Collections; | |
public class Radar : MonoBehaviour { | |
private Collider[] m_enteringRadar; | |
private Collider[] m_leavingRadar; | |
public LayerMask m_radarLayerMask; | |
public Camera m_radarCamera; | |
public Light m_lightEffect; | |
private Transform m_cameraTransform; | |
private Transform m_lightTransform; | |
private float m_angleOffSet; | |
private float m_halfExtents; | |
private Vector3 m_halfExtentsBox; | |
private float m_step; | |
private float m_delay; | |
private float m_delayDegree; | |
// private int cameraDepth; | |
// private int cameraHeight; | |
// Use this for initialization | |
void Start () { | |
m_cameraTransform = m_radarCamera.GetComponent<Transform>(); | |
m_lightTransform = m_lightEffect.GetComponent<Transform>(); | |
m_angleOffSet = 45f; | |
m_halfExtents = 100f; | |
m_halfExtentsBox = new Vector3(0.01f, m_halfExtents, m_halfExtents); | |
m_step = Mathf.PI / 64f; | |
m_delay = Mathf.PI; | |
m_delayDegree = m_delay * 180f / Mathf.PI; | |
} | |
// Update is called once per frame | |
void Update () { | |
float inAngle = m_angleOffSet; | |
float outAngle = m_angleOffSet - m_delay; | |
float inAngleDegree = inAngle * 180.0f / Mathf.PI; | |
float outAngleDegree = inAngleDegree - m_delayDegree; | |
Vector3 positionIn = new Vector3(m_cameraTransform.position.x + Mathf.Sin(inAngle) * m_halfExtents, 0f, m_cameraTransform.position.z + Mathf.Cos(inAngle)* m_halfExtents); | |
Vector3 positionOut = new Vector3(m_cameraTransform.position.x + Mathf.Sin(outAngle) * m_halfExtents, 0f, m_cameraTransform.position.z + Mathf.Cos(outAngle) * m_halfExtents); | |
// | |
// Radar effects | |
// | |
// Rotate "radar emission source" | |
m_lightTransform.rotation = Quaternion.Euler(0f, inAngleDegree, 0f); | |
// Display detected objects | |
m_enteringRadar = Physics.OverlapBox(positionIn, m_halfExtentsBox, Quaternion.Euler(0f, inAngleDegree, 0f), m_radarLayerMask, QueryTriggerInteraction.UseGlobal); | |
foreach (Collider c in m_enteringRadar) | |
{ | |
c.gameObject.GetComponent<MeshRenderer>().enabled = true; | |
} | |
// Loss detected objetcs | |
m_leavingRadar = Physics.OverlapBox(positionOut, m_halfExtentsBox, Quaternion.Euler(0f, outAngleDegree, 0f), m_radarLayerMask, QueryTriggerInteraction.UseGlobal); | |
foreach (Collider c in m_leavingRadar) | |
{ | |
c.gameObject.GetComponent<MeshRenderer>().enabled = false; | |
} | |
m_angleOffSet += m_step; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment