怪物方向浮标,怪物指向浮标

24 篇文章 0 订阅
18 篇文章 0 订阅
 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

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值