What's the difference between transform.position + offset and transform.TransformPoint(offset)
I was working a the whole day on a bug where two seemingly identical positions weren't identical at all.
Turns out that transform.position + offset and transform.TransformPoint(offset) return two different things, even when the scales are all the same.
What's the difference?
Here's my whole code:
GameObject obj = new GameObject();
obj.transform.parent = transform;
obj.transform.localPosition = new Vector3(objOffset.x, objSize.y / 2, objOffset.y);
obj.transform.localRotation = Quaternion.Euler(0f, objRotation, 0f);
Matrix4x4 cubeTransform = Matrix4x4.TRS(
transform.TransformPoint(new Vector3(objOffset.x, objSize.y / 2, objOffset.y)),
//transform.position + new Vector3(objOffset.x, objSize.y / 2, objOffset.y), // why doesnt this work ??
transform.rotation * Quaternion.Euler(0f, objRotation, 0f),
transform.lossyScale);
Matrix4x4 old = Gizmos.matrix;
Gizmos.matrix *= cubeTransform;
Gizmos.color = Color.black;
Gizmos.DrawWireCube(Vector3.zero, new Vector3(objSize.x, 0, objSize.z));
Gizmos.matrix = old;
Answer by Hellium · Nov 14, 2018 at 05:19 PM
transform.position + offset
involves two points in world space and returns a point in the world space. An example would be a camera in an RTS where the camera hovers the target regardless of target's rotation.
transform.TransformPoint(offset)
involves a single point in space and a single point in LOCAL space (according to transform) and returns a point in world space. An example would be a camera in a third person shooter. The camera is always placed behind the shoulder of the player and moves according to the player's rotation.
Your answer
Follow this Question
Related Questions
Wheels not going to the right place of wheel Colliders 0 Answers
Is this code the most efficient possible, or are there better ways to rewrite it? 0 Answers
Replicate local movement between objects 1 Answer
How to get the position of a new Instantiated prefab 0 Answers
How do I make two objects share two of the same transform values? 1 Answer