- Home /
Create a Triangle Mesh.
So I am trying to create a circle out of a triangle mesh and assign the individual triangles to a game object. My script is attached but it does not seem to be working. You may need to refresh yourself with trigonometry. (Using sin and cosine to find the x and y position).
It displays a star looking shape. Almost as if the angle is extremely obtuse.
public float radius = 1;
public int slices = 6;
public List<GameObject> gSlice = new List<GameObject>();
void Start()
{
gameObject.AddComponent("MeshFilter");
gameObject.AddComponent("MeshRenderer");
float angle = 360/slices;
for(int i = 0; i < slices; i++)
{
float nAngle = angle + (i * angle);
gSlice.Add(new GameObject());
gSlice[i].gameObject.AddComponent("MeshFilter");
gSlice[i].gameObject.AddComponent("MeshRenderer");
gSlice[i].gameObject.GetComponent<MeshFilter>().mesh.vertices = new Vector3[] {new Vector3(0, 0, 0), new Vector3(Mathf.Cos(nAngle), Mathf.Sin(nAngle), 0), new Vector3(Mathf.Cos(nAngle + angle), Mathf.Sin(nAngle + angle), 0)};
gSlice[i].gameObject.GetComponent<MeshFilter>().mesh.uv = new Vector2[] {new Vector2(0, 0), new Vector2(0, 1), new Vector2(1, 1)};
gSlice[i].gameObject.GetComponent<MeshFilter>().mesh.triangles = new int[] {0, 1, 2};
Mesh mesh = GetComponent<MeshFilter>().mesh;
mesh.Clear();
}
}
As usual, thanks in advanced. ~S
Answer by SnotE101 · Dec 23, 2014 at 03:56 AM
So I made quite a simple mistake. I calculated the angle in degrees not radians. This threw off the x and y coordinates of the triangle.
Would you $$anonymous$$d to update the code, I have done this - angle = ($$anonymous$$athf.PI/180) * angle;
but the code is not showing anything
Here you go! Thats the Unity Package for the entire project.
Your answer
Follow this Question
Related Questions
Best way to optimize mesh updating through code? 1 Answer
How does the Triangles work on Mesh? 1 Answer
How come do I keep getting this error on my voxel script 1 Answer
Failed setting triangles in my mesh 1 Answer
Mesh adaption 1 Answer