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
0
Question by keanlee · Apr 17, 2016 at 12:14 PM · camerascripting problemtransformtransform.positioncamera-look

Confuse about transform.InverseTransformPoint

Hi anyone please help... I am confuse about the use of transform.InverseTransformPoint.

I am currently following the tutorial : TANKS! Unity Tutorial - Phase 3 of 8 - Camera Control,

when come to camera setup on zoom, here is the codes:

 private float FindRequiredSize()
     {
         Vector3 desiredLocalPos = transform.InverseTransformPoint(m_DesiredPosition);
 
         float size = 0f;
 
         for (int i = 0; i < m_Targets.Length; i++)
         {
             if (!m_Targets[i].gameObject.activeSelf)
                 continue;
 
             Vector3 targetLocalPos = transform.InverseTransformPoint(m_Targets[i].position);
 
             Vector3 desiredPosToTarget = targetLocalPos - desiredLocalPos;
 
             size = Mathf.Max (size, Mathf.Abs (desiredPosToTarget.y));
 
             size = Mathf.Max (size, Mathf.Abs (desiredPosToTarget.x) / m_Camera.aspect);
         }
         
         size += m_ScreenEdgeBuffer;
 
         size = Mathf.Max(size, m_MinSize);
 
         return size;
     }


Here is my question/problem:

1st question: I don't understand about what InverseTransformPoint do to the object transform, my understanding about InverseTransformPoint from unity documentation is "transforms position from world space to local space", but isn't all object local transform is 0,0,0 ???, and I also do the test with simple script:

     void Example()
         {
             result = transform.InverseTransformPoint(transform.position);
             Debug.Log (result);
         }

and the output turn out always 0,0,0 no matter where I move the object.

2nd question: In tank! tutorial script, desiredLocalPos and targetLocalPos are both transform from world position to local position, and if local position is 0,0,0 , then why this code exist? --->

Vector3 desiredPosToTarget = targetLocalPos - desiredLocalPos;

doesn't it means "Vector3 desiredPosToTarget = 0,0,0 - 0,0,0 ???

I believe I misunderstood how InverseTransformPoint work and hopefully someone help me out. Thank you!

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
2

Answer by kubajs · Sep 16, 2016 at 02:23 PM

I guess now you probably understand the problem but somebody might be desperately searching for the answer as well so I'll try to explain a bit. You need to convert the global positions to camera rig's local space as you want to count the distance between the camera rig and your target on camera rig's local X and Y axes. Therefore you need to "transfer" the target object from global (world) position to local camera rig position. See the picture visualized in the tanks tutorial in 20:37 and then compare it with the camera rig gizmo in local space in your unity scene. Local camera Z axis is always the center point where the camera rig is pointed. Now let's focus to our target only (tank). In case tank directly intersects local Y or Z axis of the camera rig, then the X axis is 0 in local position. In case tank intersects X axis of the camera rig (in local position), then the Y and Z axis is 0 in local position. Basically visualize the tank's position in 2D instead of 3D in your head. Let say camera rig's Y axis goes through tank in 2D but in 3D it's completely outside. And that's exactly what your're looking for in the local space. Nice solution to understand the logic is to visualize it directly in unity and you will definitely understand. 1. select the camera rig (not the camera!) 2. switch to local space in scene view 3. see the Z axis is in parallel with the camera rig (and points directly to the center of camera frustrum and center of far camera plane. 4. Now see the direction of camera rig's Y and X axes. 5. Add Debug.Log method with your tank transform object converted to local position as argument of the method. E.g. Debug.Log(transform.InverseTransformPoint(m_Targets[0].position)); in the for loop of FindRequiredSize method. 6. run the game and try to intersect camera rig's Y local axis. with the tank (doesn't matter in what distance from camera). 7. When you intersect the Y local axis of the camera rig, notice Debug.Log displays (0, , ). Tank is kind of "projected" on camera rig's local Y axis now, despite in it's completely outside in world space. It means X offset is now zero. 8. When you intersect the X local axis of the camera rig, notice Debug.Log displays (, 0, 0). Play with the tank's position a lot and watch scene view and you'll find the solution fast. It doesn't matter how far in the west or east direction the tank is located (direction of X local axis), the local position Y and Z axis is always 0 in case tank intersects the camera rig's X axis and that's it.

You basically cannot count the distance in the global (world) space as in this case it doesn't make any sense. Both desiredPosition and targetPosition must be converted to the camera rig's local space. As camera's local Z axis is always in parallel with the direction camera points to, it means X and Y axis is always perpendicular to the camera's view center axis. Hope you or anybody can understand my explanation. It's difficult to express it in wolds. The simulation in unity is the best to understand the logic.

Always try to visualize the tank in your head as projected to the camera rig's gizmo and in the best in 2 dimensional world.

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 Xtankin · Jan 09, 2018 at 10:00 AM 0
Share

Thank you, you help me a lot.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How do i move an object relative to another objects axis. 2 Answers

Camera cut the sides 0 Answers

Is it possible to intercept variable alteration/function calls on an object (From within the object)? 0 Answers

Need help updating camera position to go above the player 2 Answers

(C#) Follow target (script) transform target PREFAB 0 Answers


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