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
2
Question by Stemkoski · May 21, 2013 at 06:33 PM · movementtranslate

movement -- add to position vs. Translate method

I am attempting to move an object. After rotation about the object's Y-axis via the code:

 transform.Rotate( 0, rotateSpeed * Time.deltaTime, 0 );

I can move the object correctly using the code:

 transform.position += transform.forward * 10.0f * Time.deltaTime;

which works as expected, whereas the code:

 transform.Translate( transform.forward * 10.0f * Time.deltaTime );

does not; in particular, the latter version moves the objected in unexpected ways, as if the transform.forward vector is not pointing where I am expecting it to. Can anyone explain the difference between these two approaches to movement? In particular, I was surprised that they did not have the same effect.

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

2 Replies

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

Answer by robertbu · May 21, 2013 at 06:38 PM

If you look at the reference for Transform.Translate() you will see there is an optional second parameter 'relativeTo'. The default is Space.Self, which means the movement used with be a local rather than world. You can fix the problem with your use of Translate in two ways. First you can pass a local coordinate to the Translate() function:

  transform.Translate( Vector3.forward * 10.0f * Time.deltaTime );

Note the use of 'Vector3.forward' rather than 'transform.forward'. The second way is to change the 'relativeTo' parameter:

 transform.Translate( transform.forward * 10.0f * Time.deltaTime, Space.World);


Comment
Add comment · Show 3 · 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 Stemkoski · May 21, 2013 at 07:35 PM 0
Share

To see if I am understanding correctly: the Translate method considers any vector argument to be in global coordinates, then converts it into local coordinates, but transform.forward is already in pointing in the correct direction, and so it gets unnecessarily transformed, causing it to point in the wrong direction?

Thanks for your help in understanding!

avatar image Owen-Reynolds · May 21, 2013 at 07:50 PM 1
Share

Yes. In other words, you have to correct for your forwards maybe being a diagonal, but your version double-corrected for it.

trans.Translate accounts for your personal spin, and trans.forward also accounts for your personal spin. That makes sense, since transform means "me," and saying "my forward" should mean your personal blue arrow.

Using both is like trying to account for a 3-hour time-zone difference by running through a converter program and also adding 3 hours.

avatar image robertbu · May 21, 2013 at 07:56 PM 1
Share

@Stemkoski - No. Translate() by default, considers any argument to be in local coordinates and translates it to world coordinates. 'transform.forward' is already in world coordinates. So @Owen Reynold's analogy of converting twice for a time zone change is apt.

avatar image
0

Answer by evgeny0191 · Feb 12 at 01:31 PM

If you are applying position taking into account current position of you gaming object (I mean .position += newPosition and not .position = newPosition) then you could easily test these two aproaches simpy by rotating your game object.

So basically transform.Translate will move you object using .position += (so from current position of GO) and move it to a direction where X is currently pointing (assuming you are moving your object on X axis). However if you add one more argument when calling Translate method Space.World then it will work exactly the same way as .position += newPosition and instead of using local X (where your GO's X is pointing) it will use worlds X. And so on for all the other axises, so transform.Translate takes local rotation into account when moving a game object.

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

15 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

Related Questions

Wait a fraction of a second within a Function Update() 1 Answer

How to have a GameObject follow a path with c#? 3 Answers

I need to travel the same distance in less than 1 second 1 Answer

Translate Stutter on 60fps? 0 Answers

Moving a GameObject towards a CharacterController 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