1. 程式人生 > >Unity3D中2DUI跟隨場景中3D物體

Unity3D中2DUI跟隨場景中3D物體

平面UI跟隨場景中的3D物體類似於血條的跟隨效果。

經過幾種方案的對比發現把3D物體的座標轉化為平面座標的方案是最行之有效的方法。

下面直接上程式碼

public class FollwScript : MonoBehaviour
{
public Transform gob3d;//3d物體
public RectTransform imageRec;//2dUI
public Vector3 offset;//兩個物體間的偏移量


void Update ()
{
follow();
}
    void follow(){
Vector3 screenPos = Camera.main.WorldToScreenPoint (gob3d.position);
imageRec.position = screenPos+offset;
}
}