So I need to create a plane in real time based on location of 4 different points as the vertices.
Here’s my script:
public class MeshTest : MonoBehaviour {
public GameObject[] Points;
public Material mat;
void Start() {
GameObject plane = new GameObject("Plane");
MeshFilter meshFilter = (MeshFilter)plane.AddComponent(typeof(MeshFilter));
MeshRenderer renderer = plane.AddComponent(typeof(MeshRenderer)) as MeshRenderer;
renderer.material = mat;
Mesh m = new Mesh();
m.name = "ScriptedMesh";
m.vertices = new Vector3[] {
Points[0].transform.position,
Points[1].transform.position,
Points[2].transform.position,
Points[3].transform.position,
};
m.uv = new Vector2[] {
new Vector2 (0, 0),
new Vector2 (0, 1),
new Vector2 (1, 1),
new Vector2 (1, 0)
};
m.triangles = new int[] { 0, 1, 2, 0, 2, 3};
m.RecalculateNormals();
meshFilter.mesh = m;
MeshCollider collider = plane.AddComponent(typeof(MeshCollider)) as MeshCollider;
collider.convex = true;
}
}
Creating the mesh works fine, on start I get a correctly rendered plane spanning between the Points game objects, only the mesh collider doesn’t work.
When the object is created, it’s collider is just a simple 3D box. Changing the vertices positions doesn’t do anything, neither does changing the order of commands, it seems like the component just doesn’t accept this mesh although in the inspector it looks like it was assigned correctly.
Am I doing something wrong?