public enum ImgShowType
{
ellipse,
rectangle
}
private GameObject _target;
private GameObject originPoint;
private RectTransform _img;
private float a = 0, b = 0;
private ImgShowType show_type = ImgShowType.rectangle;
private GameObject _self;
private float direction = 0, direction_new;
//内部初始化时调用
private override void OnLoadingFinish()
{
originPoint = CameraControl.MainCamera.gameObject;//摄像机
a = UnityEngine.Screen.width / 2 - 50;
b = UnityEngine.Screen.height / 2 - 50;
}
//内部初始化时调用
priate override void OnShow()
{
//!CameraControl.CheckInMainView( _target.transform.position )检测是否在屏幕外
SetIsActive( !CameraControl.CheckInMainView( _target.transform.position ) );
}
//外部设置目标和对象时调用
public void SetDate(GameObject target, GameObject self)
{
_target = target;
_self = self;
}
//在Update中判断是否在屏幕外,在就显示不在就不显示
public void SetIsActive(bool value)
{
this.GameObject.SetActive value );
}
public void Update()
{
UpdateDirAndPos( );
}
#region 更新浮标的位置和方向
private void UpdateDirAndPos()
{
var dir = _target.transform.position - _self.transform.position;
dir.y = 0;
dir = dir.normalized;
direction = Vector3.Dot( dir, originPoint.transform.forward );
Vector3 u = Vector3.Cross( dir, originPoint.transform.forward );
if (direction > 1) { direction = 1f; }
if (direction < -1) { direction = -1f; }
direction = Mathf.Acos( direction ) * Mathf.Rad2Deg;
_img.rotation = Quaternion.Euler( new Vector3( 0, 0, direction * (u.y > 0 ? 1 : -1) ) );
Vector3 result = Vector3.zero;
//Debug.Log(direction);
if (direction == 0)
{
result = new Vector3( 0, b, 0 );
}
else if (direction == 180)
{
result = new Vector3( 0, -b, 0 );
}
else
{
direction_new = 90 - direction;
float k = Mathf.Tan( Mathf.PI / 180 * direction_new );
switch (show_type)
{
case ImgShowType.ellipse:
//==========椭圆========
result.x = a * b * Mathf.Sqrt( (1.00f / (b * b + a * a * k * k)) );
result.y = k * result.x;
result.z = 0;
if (u.y > 0) { result.x = result.x * -1; }
break;
case ImgShowType.rectangle:
//======矩形=================
result.x = b / k;
if ((result.x > (a * -1)) && (result.x < a))
{//线1,3
result.y = b;
if (direction > 90)
{
result.y = b * -1;
result.x = result.x * -1;
}
}
else
{
result.y = a * k;
if ((result.y > (b * -1)) && (result.y < b))
{//线2,4
result.x = result.y / k;
}
}
if (u.y > 0)
{
result.x = result.x * -1;
}
break;
}
}
if (result != null && _img != null)
{
_img.localPosition = result;
}
}
#endregion
}
怪物方向浮标,怪物指向浮标
最新推荐文章于 2023-08-03 14:43:18 发布