- Home /
Internal coordinates for each object
Hello.
I have a question about coordinates of objects. I know, that I can get object coordinates using gameObject.transform.position. But is there any build-in method or some way to get internal coordinates? I will explain what I mean on simple example.
So I want to each object in game has its own coordinate system. For example point 0, 0, 0 should be always in the middle of object (or in the left, bottom corner - that doesn't matter) and it doesn't depend on global position of the object. Is there any way to get it?
Answer by DiegoSLTS · Aug 21, 2016 at 03:23 AM
Objects have their own coordinate system already...
They're always at zero in it's own system, and transform's forward, up and right are the x, y and z axis (I'm not sure if that's the proper order).
Any child object will move and rotate when the parent rotates, keeping it's distance and rotation unchanged relative to the parent coordinates. Transform.local* properties are all relative to the parent's space.
Even for unrelated objects you can move between local coordinates using: TransformPoint, TransformDirection, TransformVector (from local to world space),InverseTransformPoint, InverseTransformDirection and InverseTransformVector (from world to local space).
Answer by rockin · Aug 21, 2016 at 09:10 AM
I'm not sure if this is the correct way of doing it, but you can use mesh filter bounds center to get the offsets like this:
MeshFilter mf = GetComponent<MeshFilter>();
foreach (var v in mf.mesh.vertices)
{
var internalPosition = v - mf.mesh.bounds.center;
}
It might be good enough for your usecase. It will not work properly for SkinnedMeshRenderers. I think the correct way of doing it is to multiply vertex position with the World2Object matrix, but I haven't found a way to do that outside of shaders.
Transform.worldToLocal$$anonymous$$atrix, or just use Transform.InverseTransformPoint.I think the correct way of doing it is to multiply vertex position with the World2Object matrix, but I haven't found a way to do that outside of shaders.