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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Fredo-Lopez · Oct 09, 2013 at 02:43 PM · rotatetranslateforward

Rotating a cube changes its local forward?

I've got a Cube transform, and I've created my first script to make it move and rotate on key press. The script basically does this on Update():

 if (Input.GetKey(KeyCode.W))
 {
     thisTransform.Translate(thisTransform.forward * Speed * Time.deltaTime);
 }
 else if (Input.GetKey(KeyCode.S))
 {
     thisTransform.Translate(thisTransform.forward * Speed * -1f * Time.deltaTime);
 }
         
 if (Input.GetKey(KeyCode.A))
 {
     thisTransform.Rotate(thisTransform.up * Speed * -10f * Time.deltaTime);
 }
 else if (Input.GetKey(KeyCode.D))
 {
     thisTransform.Rotate(thisTransform.up * Speed * 10f * Time.deltaTime);
 }

I think this should work, but when I run the scene, it actually moves and rotates, but as soon as I rotate the Cube it begins moving slighly sideways to the right. And if I keep rotating it'll then go backwards, and then sideways to the left, and forward again and so on!

It's like the Cube's forward vector is being rotated as well. If I rotate the Cube by 90 degrees, its forward vector seems to go another 90º off its previous front-facing side, so the cube starts moving sideways (that is, its front-facing side looking at 90º from its initial position, but moving to the exact opposite direction it was facing initially...).

Sorry if my explanation is a bit confusing :P I'm pretty frustated, since this should be an easy excercise and I'm having so much problems with it...

Comment
Add comment · Show 2
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 · Oct 09, 2013 at 02:44 PM 0
Share

it's possible RotateAround can help you - one of the most useful calls in Unity.

avatar image Fredo-Lopez · Oct 09, 2013 at 04:43 PM 0
Share

I actually considered doing that, RotateAround self or something like that :P Is there such a thing?

1 Reply

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

Answer by robertbu · Oct 09, 2013 at 02:58 PM

Your problem is that Transform.Translate() uses local direction and you are supplying it a world direction. That is thisTransform.forward is the world forward. Vector3.forward will be the local forward. So to fix your problem, you can do one of:

 thisTransform.Translate(Vector3.forward * Speed * Time.deltaTime);

or:

 thisTransform.Translate(thisTransform.forward * Speed * Time.deltaTime, Space.World);

or:

 thisTransform.position += thisTransform.forward * speed * Time.deltaTime;
Comment
Add comment · Show 7 · 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 Fredo-Lopez · Oct 09, 2013 at 03:08 PM 0
Share

It works!

So, if I understand correctly, the problem is that thisTransform.forward returns a world direction and Translate expects a local direction unless you specify otherwise with the Space.World argument, right?

I thought Vector3.forward returned a global direction, but it seems to return the local forward vector... Weird, my $$anonymous$$cher's papers must be outdated or something.

avatar image robertbu · Oct 09, 2013 at 03:30 PM 0
Share

Vector3.forward is (0,0,1) can be either local or world. In a world context, it points directly towards positive 'Z'. Think about local coordinates. They are relative to the object. It is like your personal coordinate system. Your left is always your left, and your forward is your forward no matter what way you are facing. The origin of the coordinate systems is the pivot point of the object, and the x, y, and z axes are aligned with the object. So local Vector3.forward will always be the forward of the object no matter what way the object is facing.

avatar image Fredo-Lopez · Oct 09, 2013 at 03:40 PM 0
Share

I see, I get it. It's just that I thought the default context to be World, and not local! I'll keep it in $$anonymous$$d from now on :)

avatar image Fattie · Oct 09, 2013 at 04:39 PM 0
Share

it's worth realising that transform.forward (also right, etc) are utterly different things from Vector3.forward

avatar image Fredo-Lopez · Oct 09, 2013 at 04:45 PM 0
Share

Yes, I see it now. Vector3.forward just translates to Vector3(0, 0, 1), so it contains no information about anything local nor global!

Show more comments

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

Porting to Windows: Translate problem 2 Answers

Walking forward 2 Answers

Trying to make a Projectile Rotate and Move Forward.. 1 Answer

rotation in dragging 2 Answers

Can't Translate and Rotate at same time 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