Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by noflyzone · Aug 08, 2011 at 12:19 PM · differencetransformdirectiontransformpoint

Transform.TransformPoint VS TransformDirection

Transform.TransformPoint () and Transform.TransformDirection () What is the difference? I thought for a long time or did not think they have any difference?Both return an Vector3?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
23
Best Answer

Answer by Peter G · Aug 08, 2011 at 12:40 PM

That's the tricky part of Vector3's, they can store any 3 component value. So in this case, the values returned mean different things but you could interchange them if you wanted.

Transform.TransformDirection() converts a direction from local space to world space. So if you input Vector3.forward, it will return the forward vector of the character in world space. This essentially rotates the vector by the characters rotation.

Example: let's say that I want the character to move forward when I push the up arrow, but I want him to move forward on his local axis. I have to convert the local axis to world space so that it is compatible with the movement script:

 var forward = transform.TransformDirection(Vector3.forward);
 var input = Input.GetAxis("Vertical");
 transform.position += forward * input * Time.deltaTime;
 transform.Rotate( 0 , Input.GetAxis("Horizontal") * 45 * Time.deltaTime , 0 );

Transform.TransformPoint() converts a point from local space to world space. This takes the point, rotates it by the characters rotation, multiplies it by the characters scale, and then adds in the transform's position.

Example: Let's say that you want to keep the ball 2 units ahead of and 2 units to the right of the player. You use this function to set the ball at that spot.

 var relativePos = new Vector3(2 , 0 , 2 );
 var exactPos = playerTransform.TransformPoint(relativePos);
 ballTransform.position = exactPos;
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jc_lvngstn · Sep 04, 2012 at 03:25 PM 2
Share

Peter, thanks for such great examples. You explained not only what it does technically, but you also gave some great examples of how/why someone would use this. UT needs you on their documentation $$anonymous$$m :)

avatar image dogzerx2 · Jul 26, 2013 at 06:50 PM 2
Share

THAN$$anonymous$$ YOU!!!!

avatar image Coffee · Mar 30, 2014 at 04:19 AM 2
Share

Terrific explanation! This should seriously be part of the Unity documentation.

avatar image
16

Answer by Elecman · Aug 08, 2013 at 07:10 AM

Just resurrecting a 2 year old thread to make another point (no pun intended).

TransformDirection() and TransformPoint() can also be used to transform points between coordinate systems.

Quick refresher about coordinate systems: A point has 3 variables: x, y, and z. These are distances, but distances relative to what? If a point is in object space, it means that the x,y, and z distances are measured realative to the object's own origin point. If a point is in world space, it means that the x,y, and z distances are measured relative to the game world origin point.

Think of a vector as an arrow with the base at the world/object origin and the head of the arrow at the point location.

Note: object space and local space are the same thing.

In the picture below the blue point location in object space is (2, 0.5) while the point in world space is (5, 5).

alt text

 //Code is in C#
 
 //TransformPoint() transforms a position from object space to world space.
 //Both the position and the scale of your game object are taken into account
 Vector3 pointObjectSpace = new Vector3(2f, 0.5f, 0f);
 Vector3 pointWorldSpace = gameObject.transform.TransformPoint(pointObjectSpace);
 //pointWorldSpace is now (5, 5, 0)
 
 //InverseTransformPoint() transforms a position from world space to object space.
 //Both the position and the scale of your game object are taken into account
 pointWorldSpace = new Vector3(5f, 5f, 0f);
 pointObjectSpace = gameObject.transform.InverseTransformPoint(pointWorldSpace);
 //pointObjectSpace is now (2, 0.5, 0)

alt text

In the picture above the purple vector in object space has value (2, 0.5) while the vector in world space has value (0.8, 1.9). Two vectors are shown for clarity but they are identical as the position is not taken into account.

 //TransformDirection() transforms a vector from object space to world space.
 //Think of it as moving a vector from your object to the game world origin.
 //The position and scale of your game object are not taken into account.
 Vector3 vectorObjectSpace = new Vector3(2f, 0.5f, 0f);
 Vector3 vectorWorldSpace = gameObject.transform.TransformDirection(vectorObjectSpace);
 //vectorWorldSpace is now (0.8, 1.9, 0)
 
 //InverseTransformDirection() transforms a vector from world space to object space.
 //Think of it as moving a vector from the game world origin to your object.
 //The position and scale of your game object are not taken into account.
 vectorWorldSpace = new Vector3(0.8f, 1.9f, 0f);
 vectorObjectSpace = gameObject.transform.InverseTransformDirection(vectorWorldSpace);
 //vectorObjectSpace is now (2f, 0.5f, 0f)
 
 //Both TransformPoint() and InverseTransformPoint() are affected by scale. Using these 
 //functions can have unexpected results when your object does not have a uniform scale of 1.
 //If you do not want your point transformations to be affected by scale, use TransformDirection() 
 //and InverseTransformDirection() instead. These functions do not take the position of your
 //object into account, so you will have to add one line of code:
 
 //TransformDirection() is used here to transform a position from object space to world space.
 //The scale of your game object is not taken into account.
 pointObjectSpace = new Vector3(2f, 0.5f, 0f);
 pointWorldSpace = gameObject.transform.TransformDirection(pointObjectSpace);
 //pointWorldSpace is now (0.8, 1.9, 0)
 pointWorldSpace += gameObject.transform.position; //Add this to fix the lack of position.
 //pointWorldSpace is now (5, 5, 0)
 
 //InverseTransformDirection() is used here to transform a position from world space to object space.
 //The scale of your game object is not taken into account.
 pointWorldSpace = new Vector3(5f, 5f, 0f);
 pointWorldSpace -= gameObject.transform.position; //Add this to fix the lack of position.
 //pointWorldSpace is now (0.8, 1.9, 0)
 pointObjectSpace = gameObject.transform.InverseTransformDirection(pointWorldSpace);
 //pointObjectSpace is now (2f, 0.5f, 0f)

direction.png (6.6 kB)
point.png (4.9 kB)
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image vexe · Jun 02, 2014 at 01:09 AM 0
Share
  • billion!

avatar image
0

Answer by Tony-Lovell · Nov 24, 2015 at 12:10 PM

Apart from the Point/Direction issue, these functions could have more explicit names to address the "which way does it convert" issue.

Better:

TransformPointLocalToWorld(), TransformPointWorldToLocal(), TransformDirectionLocalToWorld(), TransformDirectionWorldToLocal()

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Using PositionHandle to set a a directional vector, using TransformDirection causing editor freakouts. Help! 0 Answers

Bool returns different values in Update and OnGUI 1 Answer

Quaternoin difference. (Tolerance for out of sync) 0 Answers

TransformDirection using original transform 1 Answer

transform.up returns a bad angle 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges