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
1
Question by TRON-dll · Jul 08, 2012 at 05:53 AM · rotationmovementrotate

How do I rotate a game object and have it move in relation to the rotation angle?

Hello,

I'm trying to make a rectangle turn and move, but when I move it, it moves in relation to the initial starting rotation, as opposed to the actual rotation of the object. Basically, if I rotate it 90 degrees to the right, it will still move around as if I had not done any rotation. I'm relatively new to Unity, so apologies if this is a really stupid question.

Here's my code for movement (in C#):

 if(Input.GetKey(KeyCode.W))
  moveDirection.x -= 1;
  if(Input.GetKey(KeyCode.S))
  moveDirection.x += 1;
  if(Input.GetKey(KeyCode.Space))
  moveDirection.y += 1;
  if(Input.GetKey(KeyCode.LeftAlt))
  moveDirection.y -= 1;
  if(Input.GetKey(KeyCode.D))
  transform.Rotate(Vector3.up * Time.deltaTime);
  
  newPosition = moveDirection * (moveSpeed * Time.deltaTime);
  newPosition = transform.position + newPosition;
  transform.position = newPosition;

Where newPosition and moveDirection are vector3 variables, and moveSpeed is an integer variable.

What I want to happen is have the object this script is attached to move in relation to the current rotation, so if we were to look at this from a top-down, bird's eye view, the box would move to the right if I rotated it 90 degrees so that it's facing east, it would travel to the east, or if I rotated it so that it's facing northwest, it would travel northwest, when the move forward key (W) is pressed.

As far as controls go, I'm using WSAD for forwards, backwards, left rotation, and right rotation, respectively.

I'd prefer to get help in the form of C# code, but I can probably translate anything provided in Javascript effectively. Thanks!

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

Answer by ProudOne · Jul 08, 2012 at 11:27 AM

Use transform.forward. That's the normalized Z-Axis vector of your current object.
Meaning; If you wanted to move forward by a meter:

transform.position = Input.GetAxis("Vertical") * transform.forward;

Hope that helps.

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 TRON-dll · Jul 08, 2012 at 03:58 PM 0
Share

That seems to get the object to move in the correct direction, but like you said, only by one meter. The object then goes back to where it started when I release the key. How can I have continuous movement (where the object continues to move in the desired direction until the key is let go, at which point it stops)?

EDIT: Looks like this code disables my object's vertical movement, as well. For reference, I replaced "transform.position = newPosition;" with "transform.position = Input.GetAxis("Vertical") * transform.forward;"

avatar image ProudOne · Jul 08, 2012 at 04:08 PM 0
Share

Ah, you naturally need to use += when requiring continuous motion. transform.position += Input...

Also, if you need the Object to travel anywhere else but forward, transform has up and other directional components, too. So if you wanted to travel along the Y-Axis, use transform.up.

avatar image TRON-dll · Jul 08, 2012 at 06:09 PM 0
Share

That worked, thanks! I'll post my working code as an answer for anyone else having this problem.

avatar image tuhinbhatt · Feb 28, 2013 at 04:58 PM 0
Share

how to i do the same thing but ins$$anonymous$$d using animation window i mean creating animation clip

avatar image
1

Answer by TRON-dll · Jul 08, 2012 at 07:02 PM

What I used (C#):

  //Movement
  //Move forwards and backwards
  if(Input.GetKey(KeyCode.W))
    moveDirection.x -= 1;
  if(Input.GetKey(KeyCode.S))
    moveDirection.x += 1;
  //Move up and down
  if(Input.GetKey(KeyCode.E))
    moveDirection.y += .5f;
  if(Input.GetKey(KeyCode.Q))
    moveDirection.y -= .5f;
  //Turn left and right
  if(Input.GetKey(KeyCode.D))
    gameObject.transform.Rotate(0, 90 * Time.deltaTime, 0);
  if(Input.GetKey(KeyCode.A))
    gameObject.transform.Rotate(0, -90 * Time.deltaTime, 0);
  
  //Update position
  transform.position += moveDirection.x * transform.right;
  transform.position += moveDirection.y * transform.up;

transform.right was used instead of transform.forward because of the orientation of the model I was using.

moveDirection is a Vector3 declared within the update function. If anyone has questions or suggestions, do ask.

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

gameobject stops moving correctly when rotating 1 Answer

How can I move an object to click point in 2D? 0 Answers

Updating rotation of client not working - strange error 0 Answers

How to rotate a tank turret and gun? 2 Answers

Saving Rotation of Sprite 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