- Home /
Procedurally modified mesh is displayed wrong
Hello,
i tryed to make me familary with procedural mesh generation. After modifing the "CrumpleMesh" example from the Unity-Page, i got the following problem:
I just wanted to move every plane-vertice for testing purpose 2 up. But the result ended up to be that the mesh is in place where it was at the beginning and a "clone" of the plane spawned two above the original. In the "CrumpleMesh"-example the origin-mesh is modified. Here is the code:
private Vector3[] baseVertices;
void Start()
{
renderer.material.color = Color.white;
Mesh mesh = GetComponent<MeshFilter>().mesh;
baseVertices = mesh.vertices;
Vector3[] vertices = new Vector3[baseVertices.Length];
for (int y = 0; y < mesh.vertices.Length; y++)
{
Vector3 vertex = baseVertices[y];
vertex.y += 2;
vertices[y] = vertex;
}
mesh.vertices = vertices;
mesh.RecalculateNormals();
mesh.RecalculateBounds();
}
The Code is attached to the plane. What is wrong? I hope the problem is understandable.
Thank you for your help.
Answer by Tasarran · Dec 12, 2011 at 08:39 PM
Add this line right after you RecalculateNormals
gameObject.GetComponent(MeshCollider).sharedMesh = gameObject.GetComponent(MeshFilter).mesh;
I just learned that yesterday; glad it was useful two days in a row :)
I just had a similar problem, the collider was fine, but I was not recalculating the bounds so the camera was not seeing new bounds and objects were disappearing. these are the lines I use,I can't tell what the differences compared to the above line, I think it's the same thing.
mesh.vertices = vertices;
mesh.RecalculateNormals();
mesh.RecalculateBounds();
DestroyImmediate(collider);
gameObject.AddComponent("$$anonymous$$eshCollider");
Your answer
Follow this Question
Related Questions
Determine whether or not model is "inside out" 2 Answers
Combining meshes (different materials) together for rotation/translation 1 Answer
Custom Mesh UV Problem 1 Answer
Mesh return vs out Mesh 2 Answers
Strange shading on procedural mesh 0 Answers