我正在开发2D游戏,其中某些对象具有三角形视线,并使用多边形对撞机
是否可以绘制该对撞机,使其在游戏过程中可见?
假设您的意思是PolygonCollider2D
,并且它总是以正确的顺序得到3分,我想您可以做类似的事情
using System.Linq;
...
[RequireComponent(typeof(MeshRenderer),typeof(MeshFilter),typeof(PolygonCollider2D))]
public class MeshCreator : MonoBehaviour
{
void Awake ()
{
var points = GetComponent<PolygonCollider2D>().points;
var meshFilter = GetComponent<MeshFilter>();
var mesh = new Mesh();
// Just a shorthand for something like
//var list = new List<Vector3>();
//foreach(var p in points)
//{
// list.Add(p);
//}
//mesh.vertices = list.ToArray();
mesh.vertices = points.Select(p -> (Vector3) p).ToArray();
// Here create two triangles (each 3 vertices)
// just because I don't know if you have the points in the correct order
mesh.triangles = new int[]{0,1,2,1};
meshFilter.mesh = mesh;
}
}
,然后将此组件放在PolygonCollider2D
旁边。然后它将用三角形替换该对象的网格。
您可以预先在MeshRenderer
中设置的材料。
要获得此重叠区域,最简单的解决方案是半透明材料。否则,您可能需要特殊的着色器。