欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Unity技能伤害区域判定(矩形,圆,三角形,扇形)(三)

程序员文章站 2022-07-14 08:49:40
...

欢迎加入Unity业内qq交流群:956187480

qq扫描二维码加群

Unity技能伤害区域判定(矩形,圆,三角形,扇形)(三)


 

 源码地址:https://download.csdn.net/download/qq_37310110/10886004

一. 判断目标点是否在指定的扇形内

Unity技能伤害区域判定(矩形,圆,三角形,扇形)(三) 

 public GameObject target;
    private float skillDistance = 5;
    private float skillAngle = 60;

    // Update is called once per frame
    void Update()
    {
        float distance = Vector3.Distance(transform.position, target.transform. position);
        Vector3 norVec =  transform.localPosition+new Vector3(0,0, skillDistance);
        Vector3 temVec = target.transform.position - transform.position;

        Debug.DrawLine(transform.position, norVec, Color.red);
        Debug.DrawLine(transform.position, target.transform.position, Color.green);

        if (Input.GetKeyDown(KeyCode.Q))
        {
            //计算两个向量间的夹角
            float angle = Mathf.Acos(Vector3.Dot(norVec.normalized, temVec.normalized)) * Mathf.Rad2Deg;
            if (distance <= skillDistance && angle * 2 <= skillAngle)
            {
                Debug.Log("在扇形范围内");
            }
            else
            {
                Debug.Log("不在扇形范围内");
            }
        }
    }

欢迎加入Unity业内qq交流群:956187480

qq扫描二维码加群

Unity技能伤害区域判定(矩形,圆,三角形,扇形)(三)