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
11
Question by Fearthefrozt · Oct 15, 2013 at 10:39 PM · c#transformtransform.translate

Difference between moving forward with transform.Translate vs transform.localPosition

I'm working on a simple 4x4 grid based game and am coding the scripts in C#. When I implement either of these lines of code they appear to have the same effect, I don't have them both running at the same time.

 transform.Translate(Vector3.forward*Time.deltaTime*6f);
 
 transform.localPosition += transform.forward*Time.deltaTime*6f;

I was wondering if there was a significant difference between the two or if I should use one over the other, thanks!

Comment
Add comment · Show 10
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 Benproductions1 · Oct 16, 2013 at 12:50 AM 0
Share

Correct me if I'm wrong, but isn't localPosition is just a property with it's setter as Translate?

avatar image whydoidoit · Oct 16, 2013 at 12:53 AM 0
Share

Not exactly - the Translate takes in to consideration the rotation of the object and applies that to the vector before adding it.

avatar image Benproductions1 · Oct 16, 2013 at 12:58 AM 0
Share

actually, you can set the space in which Translate moves. If you use Space.Local (or something like that) then transform.localPosition is defined as:

 function set localPosition(value:Vector3) {
     this.Translate(value, Space.Local);
 }

I think. It might be the other way around

avatar image whydoidoit · Oct 16, 2013 at 01:00 AM 0
Share

No not really - because altering localPosition with a vector does not rotate that vector based on localRotation.

avatar image whydoidoit · Oct 16, 2013 at 01:01 AM 0
Share

Put an object in a scene, rotate it so that it is at 0,90,0 then:

  transform.localPosition += Vector3.forward;

Is not the same as

  transform.Translate(Vector3.forward);
Show more comments

2 Replies

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

Answer by whydoidoit · Oct 16, 2013 at 12:50 AM

Well I always use the second format, they are indeed exactly the same presuming that there is no parent.

Translate takes into consideration the rotation of the local object, so it converts Vector3.forward into the direction of the current object.

To get exactly the same effect with or without parents, you would need to do:

  transform.position += transform.forward * ....
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 whydoidoit · Oct 16, 2013 at 12:56 AM 1
Share

Bear in $$anonymous$$d that it is marginally faster to work with localPosition compared to position - especially when the object has parents.

avatar image Fearthefrozt · Oct 16, 2013 at 01:26 AM 0
Share

Thanks for the answer I appreciate it.

avatar image
24

Answer by vexe · Jun 19, 2014 at 04:57 PM

This is what Translate literally does:

 public void Translate(Vector3 translation, [DefaultValue("Space.Self")] Space relativeTo)
 {
     if (relativeTo == Space.World)
     {
         this.position += translation;
     }
     else
     {
         this.position += this.TransformDirection(translation);
     }
 }

I really believe one's decompiling and taking a peek himself is very educational. Have doubt how XXX work? just take a look...

I use ILSpy, it served me well.

It might sound intimidating but the process goes something like this:

  1. Download and install ILSpy.

  2. Drag/drop the UnityEditor.dll located in the Editor/Data/Managed folder in your disk to ILSpy

  3. Browse through, lurk around till you find what you're looking for.

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

18 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

Related Questions

transform.Translate moving while upside down? 1 Answer

Picking up an Object and locking its position relative to parent. 1 Answer

Enemy won't collide with walls using transform.Translate 1 Answer

Truck moves sideways when using transform.forward,When i translate my truck with transform.forward it goes sideways 0 Answers

GUI.Button child of the camera . 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