Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class UILogic : MonoBehaviour {
- public LockOnScript lockOnScript;
- private Camera actorCamera;
- private Transform lockOnTarget;
- void OnGUI()
- {
- actorCamera = GetComponentInChildren<Camera>();
- lockOnTarget = lockOnScript.LockOnTarget;
- //position of target on screen using viewport (0.0-1.0, 0.0-1.0) coordinates
- //(0.0, 0.0) is top left corner of screen
- Vector3 markerCoords = actorCamera.WorldToViewportPoint(lockOnTarget.position);
- //z coordinate of markerCoords is distance of target from camera
- float depthFactor = 200.0f / markerCoords.z;
- if(lockOnTarget != null)
- {
- //if there's a target we can lock on, draw a marker over it on the screen
- //uses viewport (0.0-1.0, 0.0-1.0) coordinates, (0.0, 0.0) is top left corner of screen
- DrawCenteredVPSpaceRect(markerCoords.x, markerCoords.y, TargetMarker, depthFactor);
- }
- }
- private void DrawCenteredVPSpaceRect(float x, float y, Texture2D tex, float scale)
- {
- int texWidth = (int)(tex.width * scale);
- int texHeight = (int)(tex.height * scale);
- int xPos = (int)(x*Screen.width);
- int yPos = (int)(y*Screen.height);
- Rect texRect = new Rect(xPos, yPos, texWidth, texHeight);
- GUI.DrawTexture(texRect, tex);
- }
- }
Add Comment
Please, Sign In to add comment