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
4
Question by xEnOnn · Mar 06, 2013 at 12:12 PM · forwardconversioncoordinate

Converting between local and global direction

I'm using a forward direction vector of a gameobject like this: myGameobject.forward;

This vector however is a local direction. How can I convert direction into a global direction? And when I have a global direction, how can I convert it back to the local direction of a particular game object?

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

Answer by Bunny83 · Mar 06, 2013 at 12:20 PM

First a gameobject doesn't have a forward variable, only the Transform component of a GameObject. This forward vector is always in world space. The forward direction in local space is a constant value:

 Vector3.forward == Vector3(0,0,1)  // forward in localspace
 
 myGameobject.transform.forward     // forward in worldspace

To convert between local and world space you can use:

  • TransformDirection to transform a direction from local to world space

  • InverseTransformDirection to transform a direction from world to local space

  • TransformPoint to transform a position from local to world space

  • InverseTransformPoint to transform a position from world to local space.

Comment
Add comment · Show 2 · 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 gtucker3d · Sep 19, 2021 at 07:29 AM 0
Share

Thanks mate this was awesome information but I think you have it backwards.

Vector3.forward == Vector3(0,0,1) // forward in Worldspace

myGameobject.transform.forward // forward in Localspace

avatar image Bunny83 gtucker3d · Sep 19, 2021 at 02:26 PM 0
Share

No, it's not backwards, you may read it the wrong way ^^. Local space is a coordinate space just like world space is a coordinate space. The vector (0,0,1) is the objects forward vector in its own local space. Of course a vector is just a vector and can be interpreted in any space. However in worldspace the vector (0,0,1) would designate the worlds z axis and would not be related to any object.


In contrast transform.forward returns the objects forward axis in worldspace. That's exactly what my comments said ^^. The first paragraph in the answer actually explain that already, no? This was the whole confusion that the OP had in the first place.


edit
ps: If you have trouble understanding coordinate spaces, vectors and linear algebra, I would highly recommend to watch the 3b1b series on linear algebra.

avatar image
2

Answer by Philipp · Mar 06, 2013 at 12:15 PM

Use Transform.TransformDirection / Transform.TransformPoint and Transform.InverseTransformDirection / Transform.InverseTransformPoint!

In your case, you'd use myGameObject.transform.TransformDirection(new Vector3(0,0,1)) to convert a forward direction to a global direction. And InverseTransformDirection to convert back from global to local space.

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

Answer by ViktorCor · Nov 19, 2016 at 03:06 PM

Maybe you know?:

How to calculate InverseTransformDirection if you have not transform and have only Vector3(position) and Quartanion(rotation)?

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

14 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

Related Questions

How do you turn object to face a particular vector ? 1 Answer

GameObject facing fowards 1 Answer

controller.simplemove having issues with vector3.forward 2 Answers

Issue with Shadow Rendering 4 Answers

I need to alter the vector alignment of an object 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