Search Unity

2D On screen && off screen view C#

Discussion in '2D' started by The3DKnight, Jun 15, 2016.

  1. The3DKnight

    The3DKnight

    Joined:
    Feb 8, 2011
    Posts:
    240
    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?

    Untitled-1.jpg
    Code i found is :

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [RequireComponent (typeof (GUITexture))]
    5. public class ObjectLabel : MonoBehaviour {
    6.  
    7.     public Transform target;  // Object that this label should follow
    8.     public Vector3 offset = Vector3.up;    // Units in world space to offset; 1 unit above object by default
    9.     public bool clampToScreen = false;  // If true, label will be visible even if object is off screen
    10.     public float clampBorderSize = 0.05f;  // How much viewport space to leave at the borders when a label is being clamped
    11.     public bool useMainCamera = true;   // Use the camera tagged MainCamera
    12.     public Camera cameraToUse ;   // Only use this if useMainCamera is false
    13.     Camera cam ;
    14.     Transform thisTransform;
    15.     Transform camTransform;
    16.     public string tag = "";
    17.     GUITexture gui;
    18.  
    19.     public bool onscreen = true;
    20.  
    21.     void Start ()
    22.     {
    23.         gui = GetComponent<GUITexture>();
    24.         thisTransform = transform;
    25.         if (useMainCamera)
    26.             cam = Camera.main;
    27.         else
    28.             cam = cameraToUse;
    29.         camTransform = cam.transform;
    30.         target = GameObject.FindWithTag(tag).transform;
    31.     }
    32.  
    33.  
    34.     public Transform targetPoint;
    35.     public float holdGazeTimeInSeconds = 2;
    36.  
    37.     private float currentGazeTimeInSeconds = 0;
    38.  
    39.  
    40.  
    41.     void Update()
    42.     {
    43.         //Part i don't know
    44.  
    45.         if(/*OBJECT IS IN CAMERA VIEW*/)
    46.         {
    47.         Debug.Log("Found it");
    48.         gui.enabled = false; //Disable gui while i can see it!
    49.         }
    50.          else
    51.         {
    52.         Debug.Log("Not Found");
    53.         gui.enabled = true; //Eneble gui when i can't see it!
    54.         }
    55.  
    56.  
    57.         //Till here i need help
    58.    
    59.         if ( clampToScreen ) {
    60.                
    61.                 Vector3 relativePosition = camTransform.InverseTransformPoint (target.position + offset);
    62.                 relativePosition.z = Mathf.Max (relativePosition.z, 1.0f);
    63.                 thisTransform.position = cam.WorldToViewportPoint (camTransform.TransformPoint (relativePosition));
    64.                 thisTransform.position = new Vector3 (Mathf.Clamp (thisTransform.position.x, clampBorderSize, 1.0f - clampBorderSize),
    65.                     Mathf.Clamp (thisTransform.position.y, clampBorderSize, 1.0f - clampBorderSize),
    66.                     thisTransform.position.z);
    67.         }
    68.         else
    69.         {
    70.             thisTransform.position = cam.WorldToViewportPoint(target.position + offset);
    71.  
    72.         }
    73.     }
    74. }
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807