Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
7
Question by AntLewis · Sep 12, 2012 at 03:31 PM · world spacelocal space

Local 'forward'

Hi guys, I wondered if someone could confirm something for me. According to docs, transform.forward is forward in world space; what is it's local space equivalent? i.e. if I want an object to move in the direction of it's blue axis

Comment
Add comment · Show 4
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 Fattie · Sep 12, 2012 at 03:33 PM 0
Share

no, forward is in fact "that thing's" forward.

the way it's nose is pointing. yes they are "local" as it were.

you also have up and right available...

file:///Applications/Unity/Unity.app/Contents/Documentation/Documentation/ScriptReference/Transform.html

avatar image AntLewis · Sep 12, 2012 at 04:08 PM 0
Share

At the start my objects Z axis is aligned with world z axis. It moves forward, hits the terrain which is in front of it, turns 90 degrees to the right (as expected), but then moves down the screen rather than moving 'ahead' along its Z axis (which is correctly pointed to the right edge of screen) - anything glaringly obvious? Thanks for the help!

public class BEHAVIOUR_NAVIGATION : $$anonymous$$onoBehaviour {

// Update is called once per frame void Update () { transform.Translate (transform.forward 2.0f Time.deltaTime, Space.Self); } void OnTriggerEnter (Collider other) {

if (other.tag == "TERRAIN") { Debug.Log ("Hit terrain"); transform.Rotate (Vector3.up * 90.0f); } }

}

avatar image whydoidoit · Sep 12, 2012 at 04:11 PM 2
Share

Your problem is combining transform.Translate(xxx, Space.Self) with transform.forward. If you are using Space.Self then you want to use Vector3.forward as it is already doing that work for you.

avatar image AntLewis · Sep 12, 2012 at 04:37 PM 0
Share

Ah, understood. Thanks very much to both of you.

4 Replies

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

Answer by majakthecoder · Apr 17, 2013 at 10:18 PM

Vector3 localForward = transform.worldToLocalMatrix.MultiplyVector(transform.forward);

is that what you are looking for?

Comment
Add comment · Show 5 · 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 whydoidoit · Apr 17, 2013 at 10:20 PM 3
Share

Probably not as that is just Vector3.forward.

avatar image whydoidoit · Apr 17, 2013 at 10:23 PM 5
Share

And just as a reference - if you want to convert a direction it is much faster to use a Quaternion.

 var localDirection = transform.rotation * someWorldSpaceDirection;

 var localDirection = transform.InverseTransformDirection(someWorldSpaceDirection);


I believe this is due to the need to create the matrix - in any case:

The quaternion manipulation is 2x faster than the matrix method and the InverseTransformDirection is 8% faster than the quaternion.

avatar image Kynrus01 whydoidoit · Mar 19, 2017 at 11:13 PM 0
Share

Thank you, this is exactly what I was looking for. I also appreciate how you added how much faster each solution was :)

avatar image DBar · Aug 19, 2014 at 01:56 AM 0
Share

Ok, this one doesn't proyect a ray in local space, it projects in WORLD space. So, i was right. It is confusing.

 //var localDirection = transform.InverseTransformDirection(transform.position.down);

This one either, again, it projects in WORLD space.

//var localDirection = transform.InverseTransformDirection(transform.position.down);

The CORRECT answer to this question, for my moment being is this:

CLEAR ANSWER to Local 'forward', without CONFUSION, slow or not:

Vector3 localForward = transform.worldToLocal$$anonymous$$atrix.$$anonymous$$ultiplyVector(transform.forward);

So, majathecoder was right. Sorry whydoidoit.

avatar image DBar · Aug 19, 2014 at 02:25 AM 0
Share

Can you please convert the code that majathecoder posted with the quaternion manipulation to a forward example? I believe your last solution is just confusing, if it is more optimized sorry but i dont understand someWorldSpaceDirection is. Again, it is confusing.

The code that majathecoder posted works as expected, slow or not it is usable.

avatar image
3

Answer by hathol · Sep 12, 2012 at 04:21 PM

Just use the Vector3 constants for local space directions. (Vector3.forward, Vector3.up and Vector3.right)

Comment
Add comment · Show 5 · 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 hathol · Sep 12, 2012 at 05:34 PM 2
Share

Nope, that's forward in the objects local space :)

Vector3.forward applied to an object in world space (for example by using transform.position += Vector3.forward) will always move the object in the same direction relative to the origin, no matter the direction it is facing. Vector3 forward applied to an object in local space (for example by using transform.Translate(Vector3.forward)) will always move the object in the direction of its local forward axis

Now transform.forward does indeed give you the objects forward axis, but in world space coordinates (so relative to the origins axes). That's why all sorts of crazy things happen when you use transform.translate(transform.forward) since you're mixing coordinate systems :)

avatar image whydoidoit · Sep 12, 2012 at 05:39 PM 1
Share

I think it's nomenclature between you guys:

a) $$anonymous$$ is saying - when in the context of a function that moves an item in local space use the Vector3.xxx constants

b) Fattie is saying - when you want to move an object relative to its local coordinate system add transform.xxx to its global position

Both are correct.

Personally I never use transform.Translate so transform.forward is the local equivalent, the blue Axis the OP asks about, when added to the transform.position of the object and Vector3.forward is the same when added to the transform.localPosition

Given info from Unite12 it appears that there is a significant cost penalty in using transform.position on objects inside a hierarchy because it is internally translated to transform.localPosition.

avatar image Bunny83 · Sep 12, 2012 at 05:43 PM 0
Share

Exactly ;) A vector is just a vector. It depends how you use / interpret it. Since Transform.Translate takes a local space vector by default it's fine to use Vector3.forward.

The OP used a worldspace vector (transform.forward) as localspace vector which would of course lead to a strange behaviour ;)

It's like the "local position" of an object in it's own local-space is always (0,0,0)

avatar image Vortavasail · Sep 05, 2016 at 04:48 AM 0
Share

is there a a way to read local forward values.

a game object is set to move on its own without any in put how would you read its movements relative to its facing potions.

avatar image Ricna · Jul 23, 2018 at 01:11 PM 0
Share

It's wrong... Try transform.position += Vector3.forward to move a GameObject with rotation (0,-90,0). The "forward" will be in the X not the Z axis.

avatar image
1

Answer by Caminhoneiro_Hell_Porpeta · Apr 15, 2019 at 03:56 PM

You can use the TransformDirection to use it as local. (:

Vector3 forward = obj1.transform.TransformDirection(Vector3.forward);

Comment
Add comment · Show 4 · 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 Bunny83 · Apr 15, 2019 at 05:12 PM 0
Share

Using this line makes no sense at all as the result is exactly the same as just using

 obj1.transform.forward

I've seen many people use TransformDirection with Vector3.forward which is just a complicated (and probably slower) way to write transform.forward.

avatar image Dy4848 Bunny83 · Mar 12 at 11:06 PM 0
Share

Is there a way to use transform.forward but for the left?

avatar image Hellium Dy4848 · Mar 12 at 11:13 PM 1
Share

-transform.right

Show more comments
avatar image
0

Answer by vladibo · Sep 21, 2016 at 12:47 PM

 // point
 internal static void ToLocal(ref Vector3 worldPoint, ref Quaternion worldRotation, ref Vector3 worldPosition, out Vector3 localPoint)
 {
     localPoint = Quaternion.Inverse(worldRotation)*(worldPoint - worldPosition);
 }
 internal static Vector3 ToLocal(ref Vector3 worldPoint, ref Quaternion worldRotation, ref Vector3 worldPosition)
 {
     return Quaternion.Inverse(worldRotation)*(worldPoint - worldPosition);
 }
 internal static Vector3 ToLocal(Vector3 worldPoint, Quaternion worldRotation, Vector3 worldPosition)
 {
     return Quaternion.Inverse(worldRotation)*(worldPoint - worldPosition);
 }
 
 
 internal static void ToWorld(ref Vector3 localPoint, ref Quaternion worldRotation, ref Vector3 worldPosition, out Vector3 worldPoint)
 {
     worldPoint = worldRotation * localPoint + worldPosition;
 }
 internal static Vector3 ToWorld(ref Vector3 localPoint, ref Quaternion worldRotation, ref Vector3 worldPosition)
 {
     return worldRotation * localPoint + worldPosition;
 }
 internal static Vector3 ToWorld(Vector3 localPoint, Quaternion worldRotation, Vector3 worldPosition)
 {
     return worldRotation * localPoint + worldPosition;
 }
 
 
 // vector
 
 internal static void ToLocal(ref Vector3 worldVector, ref Quaternion worldRotation, out Vector3 localVector)
 {
     localVector = Quaternion.Inverse(worldRotation)*worldVector;
 }
 internal static Vector3 ToLocal(ref Vector3 worldVector, ref Quaternion worldRotation)
 {
     return Quaternion.Inverse(worldRotation)*worldVector;
 }
 internal static Vector3 ToLocal(Vector3 worldVector, Quaternion worldRotation)
 {
     return Quaternion.Inverse(worldRotation)*worldVector;
 }
 
 
 internal static void ToWorld(ref Vector3 localVector, ref Quaternion worldRotation, out Vector3 worldPoint)
 {
     worldPoint = worldRotation * localVector;
 }
 internal static Vector3 ToWorld(ref Vector3 localVector, ref Quaternion worldRotation)
 {
     return worldRotation * localVector;
 }
 internal static Vector3 ToWorld(Vector3 localVector, Quaternion worldRotation)
 {
     return worldRotation * localVector;
 }
 
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 vladibo · Sep 21, 2016 at 01:18 PM 0
Share

This of course is assu$$anonymous$$g there is no scale

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

22 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

Related Questions

Rotate and transform in local space 2 Answers

Problem with bullet projectiles direction 2 Answers

Changing particles in particle system from local space of skinned character mesh to world space (unattached) 0 Answers

Rigidbody constraints in local space? 7 Answers

How to Convert moveDirection.z = 20 to Local Space? 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