Skip to content

Instantly share code, notes, and snippets.

@nikhilsinha
Created January 2, 2017 01:28
Show Gist options
  • Save nikhilsinha/fcfb2c42a9913ec6124c607863f17197 to your computer and use it in GitHub Desktop.
Save nikhilsinha/fcfb2c42a9913ec6124c607863f17197 to your computer and use it in GitHub Desktop.
using UnityEngine;
public class UiUnitAttacher : MonoBehaviour
{
public GameObject Target;
private RectTransform _rectTransform;
// Use this for initialization
void Start()
{
_rectTransform = GetComponent<RectTransform>();
}
// Update is called once per frame
void Update()
{
Vector3 pos = Target.transform.position;
Vector3 vp = Camera.main.WorldToViewportPoint(pos);
vp.x = vp.x * Screen.width;
vp.y = vp.y * Screen.height;
_rectTransform.position = vp;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment