- Home /
Why is transform.InverseTransformPoint().y offset?
I have this (BAD) code :
// Were all Skin Mesh Renderers are located (only one is enabled)
public GameObject meshContainer;
// Get the enabled Skin Mesh Renderer
SkinnedMeshRenderer skinnedRenderer = meshContainer.GetComponentInChildren<SkinnedMeshRenderer>(false);
public void TestMethod(Vector3 worldPoint) {
Mesh staticMesh = new Mesh();
skinnedRenderer.BakeMesh(staticMesh);
Vector3[] vertices = staticMesh.vertices;
Vector3 localPoint = transform.InverseTransformPoint(worldPoint);
Debug.Log(worldPoint + " " + localPoint);
for (i = 0; i < vertices.Length; i++) {
DebugExtension.DebugWireSphere(vertices[i], Color.blue, 0.01f, 120, true);
}
}
FYI: DebugExtension is a free asset on the store that allows drawing additional debugging gizmos like Debug.Draw. I use it to draw the vertices from the "baked" mesh above and the weird thing is they are drawn at the right X,Z location but Y is bellow the ground!
I checked the hierarchy of the object and everthing is at (0, 0, 0) except the root gameobject which is very near (0, 1.934751e-17, 0) and the scales are all at (1, 1, 1).
Im really lost here, why are the vertices offset like that?
The goal of this is to find which vertex is near the impact position so I can modify their vertex color.
Thank you for any hint, help or trace of a solution you can provide!
I got this working but since I'm still not 100% sure what is going on I'm not making this an answer.
$$anonymous$$y code is called during FixedUpdate (makes sense since a collision is initiating it) also we are using a full body I$$anonymous$$ solution for hit reactions that may have some play in what is happening.
Whatever the case is, I fixed it with this line:
localPoint.y = localPoint.y + transform.localPosition.y
The baked mesh's vertices are still underground if drawn as is but with the adjustment everything lines up right at runtime.
Sorry but your question is too confusing any is missing some parts in between your snippets. What does the mesh and the skinned mesh renderer has to do with all this? The transform has no relation to the mesh. The transform represents a coordinate space. Note that you use the transform of the object where this script is attached to and not the transform of the object with the skinned mesh renderer (unless they are actually the same). So it's not really clear which local space you're actually using.
$$anonymous$$aybe there is some sort of rotation applied? Are you sure your gizmo mode is local (and not global) and also pivot (and not center)?
You are absolutely right, I was really confused hahaha. I modified the question above to better reflect what is going on.
I had several issues at the same time, the drifting Y value has been fixed and was my bad but the Y value of the baked mesh is still offset by - transform.localPosition.y is this normal? If I draw the mesh without baking it sits happily above ground. Same code. It's not caused by the gizmos. There are no rotation applied besides whatever the animation clip is doing.
If you are sure that your local position is offset by transform.localPosition that you just use the wrong transform. That means your local point is actually relative to the parent of your transform. However It's not clear how you actually deter$$anonymous$$e which position is the correct position. For example the wirespheres that you render are rendered in worldspace with localspace positions since the vertices of the mesh are in local space. So those will most likely not represent your actual vertex positions.