I’ve created a basic HUD radar system based on 3D objects (Plane), the script detect the enemy and follow him on screen, but my problem is ,sometimes HUD go to out of the screen area.
Here is my code:
#pragma strict
// ******Public dimentions ******
public var AircraftObject : Transform;
// ******Private dimentions ******
//Fetch CpuScript class to get some varibles
private var mainClass : CpuScript;
private var isRenderer : boolean;
private var aircraftArea : float;
//Other perfabs
private var Spd : GameObject;
private var Alt : GameObject;
private var Trg : GameObject;
//Calculating varibles
private var Distance : int;
private var Speed : int;
function Awake()
{
//Finding all objects
findObjects();
//Set PlayerScript into the mainClass
mainClass = gameObject.GetComponent(CpuScript);
}
function FixedUpdate()
{
isRenderer = mainClass.isProximity;
aircraftArea = mainClass.mainObjectArea;
transform.position.x = aircraftArea / 6;
//Set GUI's
guiControl();
getValue();
}
function guiControl()
{
Spd.GetComponent.<TextMesh>().text = Speed + " Knot";
Alt.GetComponent.<TextMesh>().text = "Enemy detected";
Trg.GetComponent.<TextMesh>().text = Distance + " Ft";
}
function getValue()
{
Distance = Vector3.Distance(AircraftObject.transform.position, mainClass.AircraftDistance) * 4;
Speed = Random.Range(100, 300);
}
function findObjects()
{
Spd = GameObject.Find("Speed");
Alt = GameObject.Find("Alt");
Trg = GameObject.Find("AimTarget");
}
it impossible to say from the code you have shown exactly why your 3d hud is going off screen.
But if you want to keep any 3d object, like your HUD, on screen at all time you will need to make sure it is being rendered onscreen by a camera at all times.
This can be done in a number of ways.
One way, would be to:
As long as you dont move this second camera then the HUD should always be displayed.
You will also need to make sure the main camera does not render the HUD.
Do this by:
You do this using the culling mask drop selector in the inspector for both your cameras.
FYI: you can also set the culling masks of the cameras in code but to do this you will need to use the bitwise operators.
Alternative methods would include, making the HUD a child of the main camera in the scene view, so that when the camera moves the HUD moves to.
However, this may lead to your HUD being obscured by other gameobjects.