Hello guys can someone help me out?
I used c# script that clamps GUITexture to edge of screen if object is not in it.
But how can i make the GUITexture disabled if camera can see the object? and enabled again if not in view?
Code i found is :
using UnityEngine;
using System.Collections;
[RequireComponent (typeof (GUITexture))]
public class ObjectLabel : MonoBehaviour {
public Transform target; // Object that this label should follow
public Vector3 offset = Vector3.up; // Units in world space to offset; 1 unit above object by default
public bool clampToScreen = false; // If true, label will be visible even if object is off screen
public float clampBorderSize = 0.05f; // How much viewport space to leave at the borders when a label is being clamped
public bool useMainCamera = true; // Use the camera tagged MainCamera
public Camera cameraToUse ; // Only use this if useMainCamera is false
Camera cam ;
Transform thisTransform;
Transform camTransform;
public string tag = "";
GUITexture gui;
public bool onscreen = true;
void Start ()
{
gui = GetComponent<GUITexture>();
thisTransform = transform;
if (useMainCamera)
cam = Camera.main;
else
cam = cameraToUse;
camTransform = cam.transform;
target = GameObject.FindWithTag(tag).transform;
}
public Transform targetPoint;
public float holdGazeTimeInSeconds = 2;
private float currentGazeTimeInSeconds = 0;
void Update()
{
//Part i don't know
if(/*OBJECT IS IN CAMERA VIEW*/)
{
Debug.Log("Found it");
gui.enabled = false; //Disable gui while i can see it!
}
else
{
Debug.Log("Not Found");
gui.enabled = true; //Eneble gui when i can't see it!
}
//Till here i need help
if ( clampToScreen ) {
Vector3 relativePosition = camTransform.InverseTransformPoint (target.position + offset);
relativePosition.z = Mathf.Max (relativePosition.z, 1.0f);
thisTransform.position = cam.WorldToViewportPoint (camTransform.TransformPoint (relativePosition));
thisTransform.position = new Vector3 (Mathf.Clamp (thisTransform.position.x, clampBorderSize, 1.0f - clampBorderSize),
Mathf.Clamp (thisTransform.position.y, clampBorderSize, 1.0f - clampBorderSize),
thisTransform.position.z);
}
else
{
thisTransform.position = cam.WorldToViewportPoint(target.position + offset);
}
}
}