Unity实现攻击范围检测并绘制检测区域实现攻击范围检测并绘制检测区域
主要介绍了Unity实现攻击范围检测并绘制检测区域,文中示例代码介绍的非常详细,具有一定的参考价值,感
兴趣的小伙伴们可以参考一下
本文实例为大家分享了Unity实现攻击范围检测并绘制检测区域的具体代码,供大家参考,具体内容如下
一、圆形检测一、圆形检测
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 圆形检测,并绘制出运行的攻击范围
/// </summary>
public class CircleDetect : MonoBehaviour {
GameObject go; //生成矩形的对象
public Transform attack; //被攻击方
MeshFilter mf;
MeshRenderer mr;
Shader shader;
void Start () {
}
void Update () {
if (Input.GetKeyDown(KeyCode.A))
{
ToDrawCircleSolid(transform, transform.localPosition, 3);
if (CircleAttack(attack,transform,3))
{
Debug.Log("攻击到了");
}
}
if (Input.GetKeyUp(KeyCode.A))
{
if (go != null)
{
Destroy(go);
}
}
}
/// <summary>
/// 圆形检测
/// </summary>
/// <param name="attacked">被攻击者</param>
/// <param name="skillPostion">技能的位置</param>
/// <param name="radius">半径</param>
/// <returns></returns>
public bool CircleAttack(Transform attacked, Transform skillPostion, float radius)
{
float distance = Vector3.Distance(attacked.position, skillPostion.position);
if (distance <= radius)
{
return true;
}
else
{
return false;
}
}
//生成网格
public GameObject CreateMesh(List<Vector3> vertices)
{
int[] triangles;
Mesh mesh = new Mesh();
int triangleAmount = vertices.Count - 2;
triangles = new int[3 * triangleAmount];
//根据三角形的个数,来计算绘制三角形的顶点顺序
//顺序必须为顺时针或者逆时针
评论1