- Home /
Mesh position incorrect....
When clicked on collider, it will draw prodedural box mesh. But the position is wrong and it appear that the vertex position is local insteand of world position. How can i fix??
private void makeLine(Vector2 inputPos)
{
if (!dragging && Mode == DragMode.MAKE_ICE && canDragSimu)
{
Ray ray = cameraComp.ScreenPointToRay(new Vector3(inputPos.x,inputPos.y,0));
RaycastHit hit;
//if(Physics.Raycast(ray,out hit,50))
if(Physics.Raycast(ray,out hit,150))
{
if(!hit.transform.CompareTag("NoIcePlaneArea"))
{
Vector3 hitPos = hit.point;
Debug.Log("hit pos " + hitPos);
iceLine = Instantiate(iceLineTemplate, new Vector3 (hitPos.x, hitPos.y,hitPos.z), Quaternion.identity) as GameObject;
mesh = new Mesh();
mfter = iceLine.AddComponent("MeshFilter") as MeshFilter;
mrer = iceLine.AddComponent("MeshRenderer") as MeshRenderer;
Transform parent = iceLine.transform;
Vector3 topLeftFront = new Vector3(hitPos.x-1f,hitPos.y+1f,hitPos.z);
Vector3 topRightFront = new Vector3(hitPos.x+1f,hitPos.y+1f,hitPos.z);
Vector3 bottomLeftFront = new Vector3(hitPos.x-1f,hitPos.y-1f,hitPos.z);
Vector3 bottomRightFront = new Vector3(hitPos.x+1f,hitPos.y-1f,hitPos.z);
newVertices = new Vector3[]{topLeftFront,topRightFront,bottomLeftFront,bottomRightFront};
newTriangles = new int[]{0,1,2,
2,1,3};
mesh.vertices = newVertices;
mesh.triangles = newTriangles;
mesh.RecalculateBounds();
mesh.RecalculateNormals();
mesh.Optimize();
mfter.sharedMesh = mesh;
mrer.sharedMaterial = customMat;
int i = mfter.sharedMesh.vertexCount;
while(i>=0)
{
Debug.LogError("vertex position : "+mfter.sharedMesh.vertices[i-1]);
i--;
}
}
}
}
}
Comment
Please format your code (help others - help yourself).
Best Answer
Answer by Paulius-Liekis · Feb 28, 2012 at 11:48 AM
You instantiate your line at hitPos and then you set positions of vertices to hitPos, so you get double hitPos offset. Try instantiating object at Vector.zero.
Your answer
Follow this Question
Related Questions
Assign a Mesh to Mesh Collider Automatically 1 Answer
creating a mesh procedurally 4 Answers
Simpler mesh over distance 1 Answer
transform.active question - Model Mesh Scripting 2 Answers
UV mapping on runtime mesh 0 Answers