Guest User

Untitled

a guest
Jan 18th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.27 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class UILogic : MonoBehaviour {
  5.  
  6.     public LockOnScript lockOnScript;
  7.    
  8.     private Camera actorCamera;
  9.     private Transform lockOnTarget;
  10.  
  11.     void OnGUI()
  12.     {
  13.         actorCamera = GetComponentInChildren<Camera>();
  14.         lockOnTarget = lockOnScript.LockOnTarget;
  15.        
  16.         //position of target on screen using viewport (0.0-1.0, 0.0-1.0) coordinates
  17.         //(0.0, 0.0) is top left corner of screen
  18.         Vector3 markerCoords = actorCamera.WorldToViewportPoint(lockOnTarget.position);
  19.         //z coordinate of markerCoords is distance of target from camera
  20.         float depthFactor = 200.0f / markerCoords.z;
  21.  
  22.         if(lockOnTarget != null)
  23.         {
  24.             //if there's a target we can lock on, draw a marker over it on the screen
  25.             //uses viewport (0.0-1.0, 0.0-1.0) coordinates, (0.0, 0.0) is top left corner of screen        
  26.             DrawCenteredVPSpaceRect(markerCoords.x, markerCoords.y, TargetMarker, depthFactor);
  27.         }
  28.     }
  29.    
  30.     private void DrawCenteredVPSpaceRect(float x, float y, Texture2D tex, float scale)
  31.     {
  32.         int texWidth = (int)(tex.width * scale);
  33.         int texHeight = (int)(tex.height * scale);
  34.         int xPos = (int)(x*Screen.width);
  35.         int yPos = (int)(y*Screen.height);
  36.         Rect texRect = new Rect(xPos, yPos, texWidth, texHeight);
  37.         GUI.DrawTexture(texRect, tex);
  38.     }
  39. }
Add Comment
Please, Sign In to add comment