码迷,mamicode.com
首页 > 编程语言 > 详细

Unity四元素运用之风向标跟随箭头

时间:2019-01-15 20:29:36      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:||   风向标   system   unity   new   mon   scree   mono   bug   

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;

}

}

Unity四元素运用之风向标跟随箭头

标签:||   风向标   system   unity   new   mon   scree   mono   bug   

原文地址:https://www.cnblogs.com/cxihu/p/10274000.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!