- Home /
How to get correct position after RecalculateBounds is called?
Hello I just have a quick question about RecalculateBounds. The example I'm using is from script reference
public void Start(){
Mesh mesh = GetComponent<MeshFilter>().mesh;
Vector3[] vertices = mesh.vertices;
int i = 0;
while(i < vertices.Length) {
vertices[i] += Vector3.up;
print(vertices[i]);
i++;
}
mesh.vertices = vertices;
GetComponent<MeshFilter>().mesh = mesh;
mesh.RecalculateBounds();
print(mesh.bounds);
}
After mesh.RecalculateBounds() is called the object center is moved to actual center of object. Ok. That's what I want. But as a side effect the transform.position is reset so if I place a simple cube at 0,0,0 and then second at the same position and assign a script to the second one, both of them have transform.position set to 0,0,0 but they're not in the same position at all. The second one is moved up by 1.0f.
So my question is: How to reset/recalculate transform.position to show the real actual position of object and so it would be showed in editor.
I don't want to recalculate each point in mesh each time I need to get real world position. I'd like my good old position to be set 0,1,0 just like the center of object the mesh.bounds points to and not 0,0,0 which is wrong.
Answer by UnityUser666 · Oct 07, 2014 at 01:12 AM
Better late than never.....probably not.
If you want to move your object while still retaining the values you'd expect to see in the editor window, just use good ole transform.position to assign a new location. If you're trying to do vertex manipulation, use transform.TransformPoint() to get the world position of the individual vert. Otherwise you'll get the location of the vert with respect to the transform of the object its attached to. I think this is right.
Your answer
Follow this Question
Related Questions
Locking gameobject to rotating floor 0 Answers
transform.localPosition problem. 0 Answers