I’m looking for a way to indicate the location of an object via a GUI indicator on the screen. So basically there will be a stationary object that the player needs to be aware of. there would be an indicator on the edge of the screen in the direction of the object. So say the object is to the left of the player, the indicator should be on the edge of the left side of the screen.
I’m not sure how to do GUI in real time, not to mention displaying a vector3 relative to screen position and other such things.
You mean like a minimap? A radar? A waypoint indicator? I’m assuming you’re making a 3D game and not something like a platformer with 3d objects.
For the minimap:
If you look at the bootcamp demo, there is a minimap created by using a second camera on a flat plane which is directly below the main world. Both cameras are projected onto the viewport, with the second camera using an alphamask.
The second way also requires math.
A radar would be easier. You could get the magnitude of the vector directions to get an idea of distance. The then you can get a direction vector. The direction vector and magnitude determines where from the center the object’s radar representation is placed. The direction and magnetude, projected onto a 2d radar, would give the direction of the object. You can project it by discarding the Y value, which is irrelevant if your level design is mostly 2D without alot of overlapping floors.
The third way is a waypoint indicator at the edge of the screen. Like seen in the old game Earthseige 2.
I haven’t haven’t that before, but here is how I’d do it. Basically you would need to create a raycast and determine if the object can be seen in the viewport even if line of sight is discarded. If it is seen you can create a gui pointer or rectile from the object’s center point which shows the player its in the direction, through walls and other objects. The rectile can show distance by being scaled by the magnetude of the distance. If the object is off the screen you can create a 2D pointer at the left and right side of the screen by determining the difference between the forward position of the camera (which I’m assuming is 1st person), and the angle between the forward direction and the object’s relative direction. Then a pointer would appear on the left or right side of screen depending on weather the player would have to rotate clockwise or counterclockwise on the XZ plane.
Thank goodness Unity3D has alot of math functions to make this task easier.
I think we are on the same page though. The game is at op down shooter, sort of like Evac city if you will.
I’ve done a lot with unity but somehow managed to escape having to use raycasts. They kind of confuse me to be honest. Any references I could use to get a bit of a better grasp? I’m pretty sure I can check if the object is within the camera view pretty easily, but its drawing the GUI in realtime relative to the position of the other object in the scene. Not only should it appear left or right, it should be able to get the exact direction of said object, sort of like a compass, but the indicator would be on the edge of the screen pointing in the direction of said object.
EDIT:
I found this code after some research, and it seems to do exactly what I wanted:
var target : Transform;
var offset = Vector3.up;
var clampToScreen = false;
var clampBorderSize = .05;
var useMainCamera = true;
var cameraToUse : Camera;
private var cam : Camera;
private var thisTransform : Transform;
private var camTransform : Transform;
function Start () {
thisTransform = transform;
if (useMainCamera)
cam = Camera.main;
else
cam = cameraToUse;
camTransform = cam.transform;
}
function Update () {
if (clampToScreen) {
var relativePosition = camTransform.InverseTransformPoint(target.position);
relativePosition.z = Mathf.Max(relativePosition.z, 1.0);
thisTransform.position = cam.WorldToViewportPoint(camTransform.TransformPoint(relativePosition + offset));
thisTransform.position = Vector3(Mathf.Clamp(thisTransform.position.x, clampBorderSize, 1.0-clampBorderSize),
Mathf.Clamp(thisTransform.position.y, clampBorderSize, 1.0-clampBorderSize),
thisTransform.position.z);
}
else {
thisTransform.position = cam.WorldToViewportPoint(target.position + offset);
}
}
@script RequireComponent(GUIText)