Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 kukuh_rahmat · Jan 28 at 01:45 PM · vector3mathbasicsvector math

localPosition vs Position Subtraction as vector direction

What is the difference between local position and world position difference?


For context, I have object B as children to object A

I'd like to keep object B near object A forward position using the algorithm below.


 // Update

 
 // var bVector = B.localPosition.normalized // this behaves strangely
 var bVector = (B.position - A.position).normalized; // this works
 
 var viewAngle = Vector3.Angle(A.forward, bVector);
 var axis = Vector3.Cross(bVector, A.forward).normalized;
 
 if (Mathf.Abs(viewAngle) > _limitViewAngle)
 {
                     transform.RotateAround(A.position, axis, 30f * Time.deltaTime);
 }



I think that it's strange that the line above doesn't work.

Shouldn't subtracting B position with A position makes it a relative position which is the same as local position?

Note: I don't have scale on both object A and B.

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
1
Best Answer

Answer by Captain_Pineapple · Jan 28 at 05:23 PM

Well basically the name says it all. One is the local position/rotation, one is the global. The only difference is the so called "frame of reference". For global positions and for objects without a parent this global frame of reference is the general global coordinate system. All positions and rotations are given in relation to the global origin.

You might notice that A.localPosition and A.position is the same since A does not have a parent. (Well that is my assumption at least)


When you have a parent to an object (like in your example B is child of A) then B has a set of local coordinates with is different to the global ones.

the local coordinates and global coordinates are linked by the parents rotation and position:

   B.position == B.localPosition * Quaternion.Inverse(A.rotation) + A.position

So this means:

Asuming we set B on the local position Vector3.forward and A on [1, 0, 1] with A.rotation = Quaterion.identity then Bs global position is [1, 0, 2]

If we now rotate A around the zAxis then the local Position of B would not change.

The global position of B would be:

   B.position == A.position + [cos(zAngle), -sin(zAngle), 0]
   where
   [cos(zAngle), -sin(zAngle), 0] is another way to write B.localPosition * Quaternion.Inverse(A.rotation)

I can only recommend to read a bit into rotation matrices. Intresting topic and helps understand a lot.

So when you take the above the yes your lines

 var bVector1 = B.localPosition.normalized // this does not work
 var bVector2 = (B.position - A.position).normalized; // this works

are indeed not the same vector as bVector1 will always be the same vector in local coordinates which you then would try to compare to vector A.forward which is in global coordinates - so in a different frame of reference.

You can make it work with local coordinates if you change this

  var viewAngle = Vector3.Angle(A.forward, bVector1);

to

   var viewAngle = Vector3.Angle(Vector3.forward, bVector1);

At this point you might see that this value that you calculated all the time between (B -A) and A.forward is actually constant if the local coordinates of B do not change.


Hope this helps. Let me know if something was not clear.

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

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

Related Questions

Change direction of vector based on which way player is facing 1 Answer

How to get a vector3 (postion) 1 unit away from another in the direction of a 3rd vector3? 2 Answers

How to get translation from matrix? 1 Answer

vector3 lerp based on 2 different floats/axes? 1 Answer

Please help, bullet drop is wrong at certain angles ? 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