- Home /
Transform.TransformPoint
In the Unity API it describes TransformPoint as follows:
Transforms position from local space to world space.
Then it lists the following example code:
// You need to assign an object to this variable in the inspector
var someObject : GameObject;
// Instantiate an object to the right of the current object
thePosition = transform.TransformPoint(2, 0, 0);
Instantiate(someObject, thePosition);
But if TransformPoint converts local coordinates to world coordinates, then how would this instantiate to the right of the game object, and not simply 2 units to the right of the World Space origin?
Answer by Jesse Anders · Oct 03, 2010 at 01:17 AM
I'm not sure if I understand your question fully, but it's exactly because TransformPoint() converts from local coordinates to world coordinates that the new object is spawned to the right of the current object.
Let's say that if you hold your right arm straight out to the side, the local-space coordinate (2, 0, 0) is at the tips of your fingers. No matter where you go or how you orient yourself, as long as you keep your arm straight out, the tips of your fingers will still be at exactly the same location in local space.
However, as you move and re-orient yourself, the world-space position of the tips of your fingers is constantly changing. This is what TransformPoint() does; it takes the local-space coordinate ((2, 0, 0) in this case) and converts it to a world-space coordinate based on the current transform for the object (the object being you in our example).
I don't know if that clears things up at all, but basically, what's described in the example you posted is indeed the expected (and correct) behavior.
It's converting the local point (2,0,0) to the world point (t.x+2, t.y+0, t.z+0), where t = the transform.
That's not what TransformPoint() does, generally speaking. If the transform happens to incorporate translation only, the end result would be as in your example, but in most cases other transformations (such as rotation and scale) will be involved as well.
Ah I see, it was more a misunderstanding of how they presented it on my part. So if I understand you correctly, and we instantiate an object at (2,0,0) in local space, but then the game object rotates 90degrees left, then we repeat the same process by instantiating again at (2,0,0), it will instantiate in a separate location (still to the right of the game object, but relative to the game objects rotation)
If we instantiate in world coordinates, that object will instantiate to the right of the object, but in the same position regardless of the game objects rotation. Correct?
Answer by lilinjie_ · Apr 20, 2018 at 07:48 AM
I think the description of the Unity manual is not detail. Transform.TransformPoint transforms position from local space to world space which the position is based on the transform local space. That's mean the position is relative to the current transform local space (the current transform is the origin point of the position).
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour
{
public GameObject someObject;
public Vector3 thePosition;
void Start()
{
// Instantiate an object to the right of the current object
thePosition = transform.TransformPoint(Vector3.right * 2);
Instantiate(someObject, thePosition, someObject.transform.rotation);
}
}
So the above example is mean convert the position 2 units right from the current transform in local space to world space then instantiate the clone at that position. So the new clone is always at the 2 units right from the current transform in local space.
Through the manual someone may think transform.TransformPoint(2, 0, 0) mean the position(2, 0, 0) in transform local space, but it is mean 2 units right from the current transform in local space. So the clone is not always at the position(2, 0, 0) in local space, but at the position 2 units right from the current transform in local space.
You can try this example may be help:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// Create Empty GameObject as the parent, change the transform anything but not the Vector3.zero
// Create a Cube as the someObject and attach the script to it
public class TransformPointExample : MonoBehaviour {
public GameObject someObject;
public Transform parent;
public Vector3 thePosition;
private void Start()
{
string log = "";
log = "Current Transform World Space# " + transform.position + ", Current Transform Local Space# " + transform.localPosition + ", Point# " + (Vector3.right * 2).ToString() + "\r\n";
thePosition = transform.TransformPoint(Vector3.right * 2);
GameObject clone = Instantiate(someObject, thePosition, someObject.transform.rotation);
clone.transform.SetParent(parent);
log += "thePosition# " + thePosition + ", Current Transform Inverse Transform# " + transform.InverseTransformPoint(thePosition);
transform.localPosition = Vector3.zero;
log += ", local position Vector3.zero Transform Inverse Transform# " + transform.InverseTransformPoint(thePosition);
Debug.Log(log);
}
}