• 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
0
Question by Dalsia · May 30, 2020 at 02:25 AM · transformtransform.transformpoint

Can someone please explain the difference between TransformPoint and InverseTransformPoint?

I've read a few different posts about this on StackOverflow and the Unity forums, but I still don't understand what's happening with these functions. Let me attempt to briefly explain what I do think I understand about transforms in Unity. I'll keep things simple by pretending we're in 2D using an empty GameObject (this way we don't have to take into account any scaling).

"World" space is the space relative to the world axis. World space should be constant throughout the project because the world space does not change. The origin of the world is at position (0, 0) in the world. If something is at position (0, 1) in world space, it will be 1 unit above the world origin.

"Local" space is the space that is relative to a GameObject. Essentially, the "origin" of the local space is the GameObject's center. So, if a GameObject's center point is at point (5, 5) in the world, any new objects transformed around it locally will be transformed around this point; in other words, we can think of (5, 5) as the center of the "world" of that GameObject. If I wanted to have a new object be 1 unit above this GameObject, it would be at position (6, 5) in the world, but would be at position (0, 1) in local space.

Hopefully I'm correct about that stuff, but if not I welcome any clarification. Here's some code (I will now be using two 1x1 boxes):

 public class GreenBox : MonoBehaviour
 {
     [SerializeField] private GameObject redBox;
 
     private void Update()
     {
         redBox.transform.position = new Vector2(2, 0);
 
         Vector2 position = new Vector2(0, 1);
         position = redBox.transform.TransformPoint(position);
 
         transform.position = position; // Move the green box to "position"
     }
 }


Transform.TransformPoint "transforms position from local space to world space;" in my code, "position" is the new Vector2(0, 1). This is what results if I move the redBox to position (2, 0) in world space:

alt text
If I use the same code, but switch to redBox.transform.InverseTransformPoint(position) (which says that it "transforms position from world space to local space"), and keep the redBox at position (2, 0) in the world, this is what happens:

alt text
Based on the descriptions of these methods, I have no idea what's going on with my short block of code. I don't understand how, in the case of Transform.TransformPoint, my "position" of (0, 1) is transformed from local space to world space (which is supposed to be constant throughout the world) by being above the redBox at all times; wouldn't that be local? Likewise, it seems that InverseTransformPoint is sort of a mirror image of the world space position of the redBox; I thought that this method would behave as the first one does, since it transforms a point in world space (0, 1) to local space (the position of the redBox). I understand that these methods are being called from the transform of the redBox, but it just doesn't make sense to me.

Am I doing something wrong? Any help would be appreciated!

transformpoint.png (30.7 kB)
inversetransformpoint.png (30.8 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by SteenPetersen · May 30, 2020 at 04:40 AM

If you run this code;

 [SerializeField] GameObject someObject;
 
 void Start()
 {
     // Instantiate an object to the right of the current object
     Vector3 thePosition = transform.TransformPoint(2, 0, 0);
     Instantiate(someObject, thePosition, someObject.transform.rotation);
 
     // Instantiate an object to the right but inverted of the current object
     Vector3 theInversePosition = transform.InverseTransformPoint(2, 0, 0);
     Instantiate(someObject, theInversePosition, someObject.transform.rotation);
 }


You'll see that Transform.TransformPoint does is add the position of the transform and thePosition together, which explains your green box being at the position of the red box plus the position you gave it.

Transform.InverseTransformPoint subtracts the position of the transform and then adds the theInversePosition. resulting in your green box first going from 0,0 to -2,0 and then adding the 0,1 .Takes some getting used to.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

163 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

transform.transformpoint not returning correct value 1 Answer

Comparing World Positions 1 Answer

Accessing children of instances vs children of original prefab 1 Answer

Setting parent of instantiated object fails (Error: setting parent of prefab is disabled...) 1 Answer

getting object local direction? 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