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
0
Question by Kennyist · Oct 10, 2013 at 01:21 AM · c#rotationmovementtransformmove an object

Move relative to objects rotation (C#)

How would you make an object move based on the objects rotation, I have this:

 if(engineReady){
         float tempPitch = pitch / 1.66666f;
         tempPitch = (tempPitch - 30.0f) / 100.0f;
         pitchDegrees = tempPitch;
         heliSpeed = Mathf.Abs((tempPitch * 100.0f));
         
         heli.transform.rotation = new Quaternion(pitchDegrees, heli.transform.rotation.y, heli.transform.rotation.z, heli.transform.rotation.w);
         
         if(curHeight < 0.01){ heliSpeed = 0.0f; pitch = 50; }
         
         if(pitch > 50){
             heli.transform.position += Vector3.forward * (heliSpeed * Time.deltaTime);
         } else {
             heli.transform.position -= Vector3.forward * (heliSpeed * Time.deltaTime);
         }
         
     }
     
     // ===== Yaw ======= //
     
     if(Input.GetKey(KeyCode.Z)){
         if(engineReady){
             heli.transform.rotation = new Quaternion(heli.transform.rotation.x, heli.transform.rotation.y + 0.05f, heli.transform.rotation.z, heli.transform.rotation.w);
         }
     }
     
     if(Input.GetKey(KeyCode.C)){
         if(engineReady){
             heli.transform.rotation = new Quaternion(heli.transform.rotation.x, heli.transform.rotation.y - 0.05f, heli.transform.rotation.z, heli.transform.rotation.w);
         }
     }

But .forward always follows the global Z axis.

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

1 Reply

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

Answer by clunk47 · Oct 10, 2013 at 01:24 AM

transform.forward

Comment
Add comment · Show 6 · 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 Kennyist · Oct 10, 2013 at 01:28 AM 1
Share

Ah, Thanks.

avatar image clunk47 · Oct 10, 2013 at 01:29 AM 0
Share

No problem ;)

avatar image vexe · Oct 10, 2013 at 01:40 AM 3
Share

Just filling in the OP with more info: transform.right, transform.forward and transform.up are your object's local standard basis vectors (they are also unit vectors, meaning they have a length of 1) (Btw if you're not familiar with standard basis vectors, they're actually the i along the x and j the y in a 2d coord Cartesian system). Those vectors will always rotate along with your object. Think of transform.forward as a vector pointing out of your chest (or your mouth, if you wanna stick to the head), transform.up out of your head and transform.right out of your right ear :D

Vector3.up, Vector3.forward and Vector3.right however work independently of your object - they're global to the world. Imagine standing in a room's corner such that at first both your local and the room's global axis are aligned, if you rotate, your local axis will rotate with you, but the room's won't :D

avatar image clunk47 · Oct 10, 2013 at 01:43 AM 2
Share

Just to add, if you want to use back, you would use -tranform.forward, left will be -transform.right, down will be -transform.up. You can get the same results with TransformDirection.

 transform.TransformDirection(Vector3.forward);
 
 transform.TransformDirection(Vector3.back);
 
 //and so on...

avatar image vexe · Oct 10, 2013 at 03:31 AM 1
Share

Correct. It's also interesting to mention that the docs actually got it wrong. (which is typical)

Transforms direction from local space to world space.

It's actually the opposite, TransformDirection transforms a Vector from global to local space. (They actually gave a correct example with cameraRelativeRight, so they kinda said something but did otherwise) I just tested it out, create an empty scene, with a cube and move it with:

 void Update()
 {
       void Update()
     {
         float h = Input.GetAxis("Horizontal") * speed * Time.deltaTime;
         float v = Input.GetAxis("Vertical") * speed * Time.deltaTime;

 //        _transform.localPosition += _transform.right * h;
 //        _transform.localPosition += _transform.forward * v;

         Vector3 RIGHT = _transform.TransformDirection(Vector3.right);
         Vector3 FORWARD = _transform.TransformDirection(Vector3.forward);

         _transform.localPosition += RIGHT * h;
         _transform.localPosition += FORWARD * v;
     }
 }

RIGHT and FORWARD are exactly the same as transform.right and transform.forward. Both will move the cube relative to its local axis.

And since that doc is blown, same is true for the opposite.

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

17 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

Related Questions

Flip over an object (smooth transition) 3 Answers

Making a bubble level (not a game but work tool) 1 Answer

3D Heat Seeking Missile (C#) - Odd problem 1 Answer

Push the player forward C# 0 Answers

How to allow movement of game object to certain points only 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