Hey all,
I’ve been looking at this for too long now. I’ve been working on a radar and I am trying to turn my blips on and off using a Coroutines, but I can’t seem to get them to turn back on. I’m using a Corountine and to set the alpha colour of the blips and then setting the fadeBlips boolean to (whatever it isn’t using the =! operator), however that work work. If I just set the boolean to false at the end of the corountine then I can’t seem to find a way to get it set back to true correctly.
Would I be better off using two coroutines instead? I’ve attached the entire script.
using UnityEngine;
using System.Collections;
public class Sonar : MonoBehaviour {
public Texture Enemy, PowerUp, TreasureChest;
public Texture radarBG;
public Transform centerObject; // Player Position
public float mapScale = 1.0f;
public Vector2 mapCenter;
public float maxDist = 110.0f;
private Vector3 translation = Vector3.zero;
private Quaternion rotation = Quaternion.identity;
private Vector3 scale = new Vector3(Screen.width,Screen.height,1);
private Matrix4x4 matrix;
private Rect screenPosition;
private float rotAngle;
private float angletoPlayer = 110.0f; // 40 degree angle
private bool fadeBlips = false;
void OnGUI() {
mapCenter = new Vector2(Screen.width-200-radarBG.width,Screen.height-200-radarBG.height);
matrix.SetTRS(translation, rotation, scale);
screenPosition = new Rect(Screen.width-256,Screen.height-256,256,256);
//Rotate Texture around pivot point
rotAngle += 1.0f;
GUIUtility.RotateAroundPivot(rotAngle,screenPosition.center);
GUI.DrawTexture(screenPosition,radarBG);
GUIUtility.RotateAroundPivot(-rotAngle,screenPosition.center);
//Fade out the blips over time, based off their angle to the player
if (fadeBlips) {
StartCoroutine(FadeBlips(0.0f, 1.0f, 2.0f, 5.0f)); //Fade up
} else if (fadeBlips == false) {
StartCoroutine(FadeBlips(1.0f, 0.0f, 2.0f, 5.0f)); //Fade down
}
DrawBlips("Enemy");
DrawBlips("PowerUp");
DrawBlips("TreasureChest");
}
private void DrawBlips(string tagName) {
// Find all game objects with tag
GameObject[] gos = GameObject.FindGameObjectsWithTag(tagName);
// Iterate through all game objects
foreach (GameObject go in gos) {
if (tagName == "Enemy") {
drawBlip(go,Enemy);
} else if(tagName == "PowerUp") {
drawBlip(go,PowerUp);
} else if (tagName == "TreasureChest") {
drawBlip (go, TreasureChest);
}
}
}
private void drawBlip(GameObject go, Texture aTexture) {
Vector3 centerPos = centerObject.position;
Vector3 extPos = go.transform.position;
//Get the distance of the enemy from the player
float dist = Vector3.Distance(centerPos,extPos);
float dx = centerPos.x - extPos.x; // how far to the side of the player is the enemy?
float dz = centerPos.z - extPos.z; // how far in front or behind the player is the enemy?
// what's the angle to turn to face the enemy - compensating for the player's turning?
float deltay = Mathf.Atan2(dx,dz) * Mathf.Rad2Deg - 270 - centerObject.eulerAngles.y;
// just basic trigonometry to find the point x,y (enemy's location) given the angle deltay
float bX = dist * Mathf.Cos(deltay * Mathf.Deg2Rad);
float bY = dist * Mathf.Sin(deltay * Mathf.Deg2Rad);
bX = bX / mapScale; // scales down the x-coordinate by half so that the plot stays within our radar
bY = bY / mapScale; // scales down the y-coordinate by half so that the plot stays within our radar
if (dist <= maxDist) {
//Diameter of our largest radar circle
GUI.DrawTexture(new Rect(screenPosition.center.x + bX, screenPosition.center.y + bY, 5, 5), aTexture, ScaleMode.ScaleToFit,true, 1.0f);
}
}
private IEnumerator FadeBlips(float startLevel, float endLevel, float duration, float waitTime) {
float speed = 1.0f / duration;
Color colour = GUI.color;
for (float t = 0.0f; t < 1.0f; t += Time.deltaTime * speed) {
colour.a = Mathf.Lerp(startLevel, endLevel, t);
GUI.color = colour;
}
yield return new WaitForSeconds (waitTime);
fadeBlips =! fadeBlips;
}
}