Compass/Objective System

Hey guys,I am new to javascript.

I am making a game but I need a objective system so that the player won’t go lost.

when you are looking at the object(point at object)
[19833-untitled+2.jpg|19833]

when you aren’t looking at the object(not pointing)

[19834-untitled+3.jpg|19834]

Hope you can answer my question.

You have three technical problems here: 1) knowing when an object is seen, 2) hiding/showing the arrow, and 3) pointing the arrow at the object.

Being Seen

Being seen can be handled in multiple different ways. Here is a link to a list of solutions:

http://answers.unity3d.com/questions/8003/how-can-i-know-if-a-gameobject-is-seen-by-a-partic.html

If you have only a single camera, the Renderer.isVisible is a nice solution. If you have more than one camera, then the script here will solve your problem:

http://docs.unity3d.com/Documentation/ScriptReference/GeometryUtility.TestPlanesAABB.html

There are a couple of less precise ways. You can create an angle threshold between where you are looking and the target as a test if something can be seen:

if Vector3.Angle(transform.forward, target.position-transform.position) < someThreshold) {
    Debug.Log("I can be seen");
}

Another solution which will only tell if the pivot point is visible is to convert the position to a viewport coordinate and see if it is in the range of 0.0 to 1.0 on the x and y:

var pos = Camera.main.WorldToViewportPoint(transform.position);
if (pos.x >= 0.0 && pos.x <=  1.0 && pos.y >= 0.0 && pos.y <= 1.0) {
    Debug.Log("I can be seen");
}

Point at the object

You need to construct your arrow object so that when the rotation is (0,0,0), the arrow is pointing towards positive ‘z’. Then the code to point the arrow will be:

var pos = target.position - transform.position;
pos.y = 0;
transform.rotation = Quaternion.LookRotation(pos);

Showing/Hiding the Arrow

As for showing and hiding the arrow, there are multiple different ways. You can swap the texture, hid the arrow to reveal an underlying object, or swap objects. You can use Renderer.enabled to make an object visible/invisible. You can use Renderer.material.mainTexture to assign textures to a material.