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

版权声明:转发,使用请留言告知 https://blog.csdn.net/qq_37310110/article/details/85341214

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

一. 判断目标点是否在指定的矩形内(两个随机点确定的矩形)

定义矩形的实例类

public class Rectangle01
{
    /// Min为当前坐标系中矩形的最小点
    /// Max为当前坐标系中矩形的最大点
    public Vector2 Min;
    public Vector2 Max;

    public bool Contains( Vector2 point)
    {
        if (point.x < this.Min.x)
        {
            return false;
        }
        if (point.x > this.Max.x)
        {
            return false;
        }
        if (point.y < this.Min.y)
        {
            return false;
        }
        if (point.y > this.Max.y)
        {
            return false;
        }
        return true;
    }

    public void CalcVertices(out Vector2 vertex0, out Vector2 vertex1, out Vector2 vertex2, out Vector2 vertex3)
    {
        vertex0 = this.Min;
        vertex1 = new Vector2(this.Max.x, this.Min.y);
        vertex2 = this.Max;
        vertex3 = new Vector2(this.Min.x, this.Max.y);
    }
}

测试     脚本

public class TestRt01 : MonoBehaviour {

    public Transform Point;
    public Transform RtPoint0, RtPoint1;
    public Rectangle01 rtBox;

    private const float _pointRadius = 0.1f;
  
    private void OnDrawGizmos()
    {
        if (rtBox != null)
        {
            DrawRt(rtBox);
            Gizmos.DrawSphere(Point.position, _pointRadius);
        }
    }
    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Q))
        {
            rtBox = CreateFromTwoPoints(RtPoint0.position, RtPoint1.position);
            bool cont = rtBox.Contains(Point.position);
            if (cont)
            {
                Debug.Log("目标点在矩形范围内");
            }
            else
            {
                Debug.Log("目标点不在矩形范围内");
            }
        }
    }
    public Rectangle01 CreateFromTwoPoints(Vector2 point0, Vector2 point1)
    {
        Rectangle01 rt = new Rectangle01();
        if (point0.x < point1.x)
        {
            rt.Min.x = point0.x;
            rt.Max.x = point1.x;
        }
        else
        {
            rt.Min.x = point1.x;
            rt.Max.x = point0.x;
        }
        if (point0.y < point1.y)
        {
            rt.Min.y = point0.y;
            rt.Max.y = point1.y;
        }
        else
        {
            rt.Min.y = point1.y;
            rt.Max.y = point0.y;
        }
        return rt;
    }

    protected void DrawRt( Rectangle01 box)
    {
        Vector2 v0, v1, v2, v3;
        box.CalcVertices(out v0, out v1, out v2, out v3);
        Gizmos.color = Color.blue;
        Gizmos.DrawLine(v0, v1);
        Gizmos.DrawLine(v1, v2);
        Gizmos.DrawLine(v2, v3);
        Gizmos.DrawLine(v3, v0);
    }
}

结果

二.判断目标点是否在某一矩形区域(以任一点为中心的矩形区域)

定义矩形

public class Box {

    public Vector2 Center;
    public Vector2 Axis0;
    public Vector2 Axis1;
    public Vector2 Extents;

    public Box(Vector2 center, Vector2 axis0, Vector2 axis1, Vector2 extents)
    {
        this.Center = center;
        this.Axis0 = axis0;
        this.Axis1 = axis1;
        this.Extents = extents;
    }
    public bool Contains(Vector2 point)
    {
        Vector2 vector;
        vector.x = point.x - this.Center.x;
        vector.y = point.y - this.Center.y;
        float num = vector.Dot(ref this.Axis0);
        if (num < -this.Extents.x)
        {
            return false;
        }
        if (num > this.Extents.x)
        {
            return false;
        }
        num = vector.Dot(ref this.Axis1);
        if (num < -this.Extents.y)
        {
            return false;
        }
        if (num > this.Extents.y)
        {
            return false;
        }
        return true;
    }

    public void CalcVertices(out Vector2 vertex0, out Vector2 vertex1, out Vector2 vertex2, out Vector2 vertex3)
    {
        Vector2 vector = (Vector2)(this.Axis0 * this.Extents.x);
        Vector2 vector2 = (Vector2)(this.Axis1 * this.Extents.y);
        vertex0 = (this.Center - vector) - vector2;
        vertex1 = (this.Center + vector) - vector2;
        vertex2 = (this.Center + vector) + vector2;
        vertex3 = (this.Center - vector) + vector2;
    }

}

public static class Vector2ex
{
    public static float Dot(this Vector2 vector, ref Vector2 value)
    {
        return ((vector.x * value.x) + (vector.y * value.y));
    }
}

测试脚本

public class TestBox : MonoBehaviour {

    public Transform Point;
    public Transform rtBox;
    Box box;
    private const float _pointRadius = .11f;
    private void OnDrawGizmos()
    {
        if (box!=null)
        {
            Gizmos.DrawSphere(Point.position, _pointRadius);
            DrawBox(ref box);
        }
    }
    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Q))
        {
            box = new Box(rtBox.position, rtBox.right, rtBox.up, rtBox.localScale); ;
            bool cont = box.Contains(Point.position);
            if (cont)
            {
                Debug.Log("目标点在矩形范围内");
            }
            else
            {
                Debug.Log("目标点不在矩形范围内");
            }
        }
    }

    protected void DrawBox(ref Box box)
    {
        Vector2 v0, v1, v2, v3;

        box.CalcVertices(out v0, out v1, out v2, out v3);
        Gizmos.color = Color.blue;
        Gizmos.DrawLine(v0, v1);
        Gizmos.DrawLine(v1, v2);
        Gizmos.DrawLine(v2, v3);
        Gizmos.DrawLine(v3, v0);
    }
}

结果

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

猜你喜欢

转载自blog.csdn.net/qq_37310110/article/details/85341214
今日推荐