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

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

程序员文章站 2022-03-22 14:59:04
欢迎加入Unity业内qq交流群:956187480qq扫描二维码加群源码地址:https://download.csdn.net/download/qq_37310110/10886004一. 判断目标点是否在指定的扇形内 public GameObject target; private float skillDistance = 5; private float skillAngle = 60; // Update is called on......

欢迎加入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技能伤害区域判定(矩形,圆,三角形,扇形)

本文地址:https://blog.csdn.net/qq_37310110/article/details/110131107