using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WindVaneCom : MonoBehaviour
{
public static WindVaneCom instance
{
get;
private set;
}
public bool isOn = false;
public Camera cam;
public GameObject T;
public GameObject V;
public float PivotXY = 75f;
public float scoX = 220f;
public float scoY = 270f;
private float AtX;
private float AtY;
private Vector2 v2 = Vector2.one;
private Vector2 v2new = Vector2.one;
private Vector3 v3 = Vector3.one;
private Quaternion qua = Quaternion.identity;
void Awake()
{
instance = this;
}
public void SetGameobj(Camera ca, GameObject v, GameObject t)
{
cam = ca;
T = t;
V = v;
}
// Update is called once per frame
void Update()
{
if (isOn)
{
ObjectCom com = CreatureModel.Instance.GetCurrentObjCom();
if (com != null)
{
T = com.gameObject;
}
if (V == null || T == null)
{
Debug.Log("V==null||T==null");
return;
}
if (null != Camera.main)
{
cam = Camera.main;
v2 = cam.WorldToScreenPoint(T.transform.position);
}
else
{
Debug.Log("null == Camera.main");
// cam = Camera.current;
}
if ((v2.x >= (0 - scoX) && v2.x <= (Screen.width + scoX)) && (v2.y >= (0 - scoY) && v2.y <= (Screen.height + scoY)))
{
V.SetActive(false);
}
else
{
V.SetActive(true);
}
if (v2.x < -scoX)
{
AtX = PivotXY;
if (v2.y > 0 && v2.y < Screen.height)
{
AtY = v2.y;
V.SetActive(true);
}
}
if (v2.x > (Screen.width + scoX))
{
AtX = Screen.width - PivotXY;
if (v2.y > 0 && v2.y < Screen.height)
{
AtY = v2.y;
V.SetActive(true);
}
}
if (v2.y < -scoY)
{
AtY = PivotXY;
if (v2.x > 0 && v2.x < Screen.width)
{
AtX = v2.x;
V.SetActive(true);
}
}
if (v2.y > (Screen.height + scoY))
{
AtY = Screen.height - PivotXY;
if (v2.x > 0 && v2.x < Screen.width)
{
AtX = v2.x;
V.SetActive(true);
}
}
v2new.x = AtX;
v2new.y = AtY;
V.GetComponent<RectTransform>().anchoredPosition = v2new;
qua.SetLookRotation(V.transform.position, T.transform.position);
v3 = qua.eulerAngles;
// V.transform.rotation = Quaternion.Euler(0, 0, v3.z);
V.transform.rotation = Quaternion.Euler(v3.x, 0, v3.z);
}
else
{
if (V != null)
V.SetActive(false);
}
}
void OnDestory()
{
instance = null;
}
}
原文地址:https://www.cnblogs.com/cxihu/p/10274000.html