Skip to content

Instantly share code, notes, and snippets.

@andyman
Created September 16, 2014 20:24
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save andyman/767c6b60f49fb9641a3d to your computer and use it in GitHub Desktop.
ShowScreenArrow.cs
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class ShowScreenArrow : MonoBehaviour {
public Transform target;
RectTransform rect;
Image image;
void Awake() {
rect = GetComponent<RectTransform>();
image = GetComponent<Image>();
}
// Update is called once per frame
void Update () {
Camera mainCamera = Camera.main;
float marginX = rect.rect.width/2;
float marginY = rect.rect.height/2;
float maxWidth = Mathf.Abs(mainCamera.pixelWidth);
float maxHeight = Mathf.Abs (mainCamera.pixelHeight);
Vector3 screenPoint = mainCamera.WorldToScreenPoint (target.position);
bool imageVisible = screenPoint.x <= marginX
|| screenPoint.x >= (maxWidth-marginX)
|| screenPoint.y <= marginY
|| screenPoint.y >= (maxHeight-marginY);
screenPoint.x = Mathf.Min (Mathf.Max (screenPoint.x, marginX), maxWidth - marginX);
screenPoint.y = Mathf.Min (Mathf.Max (screenPoint.y, marginY), maxHeight - marginY);
float angle = Mathf.Atan2 (screenPoint.y-maxWidth/2, screenPoint.x - maxHeight/2) * Mathf.Rad2Deg;
rect.rotation = Quaternion.Euler (0,0,angle);
rect.position = screenPoint;
image.enabled = imageVisible;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment