So I scripted a mesh, which looks like this:
(Yellow spheres are the vertices)(I named it “Ground”)
![1]
I only need the drawn upside part of the mesh for
this question
I also added a mesh collider to it and then created a new gameobject with a rigidbody attached to it.
They collide fine
I want to create a new vertex at exactly the position where they collided. So I used the OnCollisionStay()
function:
void OnCollisionStay(Collision collision)
{
ContactPoint contact = collision.contacts[0];
if (contact.otherCollider.name == "Ground")
{
//Process of collision vertex creation
newVertices = new Vector3[mesh.vertexCount + 1];
for (int i = 0; i < mesh.vertexCount; i++)
{
newVertices _= mesh.vertices*;*_
}
//sets the position of the new vertex
newVertices[mesh.vertexCount] = contact.point;
//applies changes
mesh.vertices = newVertices;
mesh.RecalculateNormals();
mesh.RecalculateBounds();
}
}
But I can’t seem to get what I did wrong
Instead of creating the vertex exactly where the collision happened, they spawn in wrong places. (See below)
![part2][2]
My guess is, that it has to do with the local and world spaces. I already tried different ways like using the transform.TransformPoint()
and similar to convert the contact point I created to relevant coordinates, but nothing.
What did I do wrong? Can you help me?
[1]: /storage/temp/92144-unity-question.png
[2]: /storage/temp/92147-unity-question-part-3.png