原本库中只有矩形RVO碰撞体,如果要添加自己的需要继承RVOObstacle抽象类,重写里面的方法
例如下面的圆柱形碰撞
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
using Pathfinding.RVO;
#endif
namespace Pathfinding.RVO
{
[AddComponentMenu("Pathfinding/Local Avoidance/Cylinder Obstacle")]
public class RVOCylinderObstacle : RVOObstacle
{
/// <summary>Height of the obstacle</summary>
public float height = 1;
/// <summary> 边数 </summary>
[Range(5, 12)]
public int edge = 5;
/// <summary>
/// 半径
/// </summary>
public float radius = 1f;
/// <summary>Center of the square</summary>
public Vector2 center = Vector3.zero;
protected override bool StaticObstacle { get { return false; } }
protected override bool ExecuteInEditor { get { return true; } }
protected override bool LocalCoordinates { get { return true; } }
protected override float Height { get { return height; } }
//If UNITY_EDITOR to save a few bytes, these are only needed in the editor
#if UNITY_EDITOR
[Range(5, 12)]
private int _edge = 5;
private Vector2 _center;
private float _height;
private float _radius;
#endif
protected override bool AreGizmosDirty()
{
#if UNITY_EDITOR
bool ret = _edge != edge || _radius != radius || _height != height || _center != center;
_edge = edge;
_center = center;
_height = height;
_radius = radius;
return ret;
#else
return false;
#endif
}
protected override void CreateObstacles()
{
edge = Mathf.Clamp(edge, 5, 12);
height = Mathf.Abs(height);
if(radius<0) radius = 0.1f;
var verts = new Vector3[edge];
float p = 360f / edge;
float angle = 0f;
for (int i = 0; i < verts.Length; i++)
{
float x = center.x + radius * Mathf.Cos(angle * Mathf.PI / 180);
float z = center.y + radius * Mathf.Sin(angle * Mathf.PI / 180);
verts[i] = new Vector3(x,0,z);
angle += p;
}
AddObstacle(verts, height);
}
}
}
#if UNITY_EDITOR
namespace Pathfinding {
[CustomEditor(typeof(RVOCylinderObstacle))]
[CanEditMultipleObjects]
public class RVOCylinderObstacleEditor : Editor {
public override void OnInspectorGUI () {
DrawDefaultInspector();
}
}
}
#endif
![](https://img-blog.csdnimg.cn/20210326104649454.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2JpbmdoZWxpZWZlbmc=,size_16,color_FFFFFF,t_70)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)