Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Formigaz · Sep 18, 2017 at 12:43 AM · rotationrotate objectrotation axis

The object turn but the axis dont (SOLVED)

Hello,

Im making a project where i use the "w,a,s,d" to move the object and the "g,j" to turn the player in the y axis. this works but when i make a 360 degrees turn my z axis stay in the same position so instead using the w key to walk i have to use the s key.

The code i'm using:

     void FixedUpdate() {
         moveInput = new Vector3(Input.GetAxisRaw("HorizontalP1"), 0, Input.GetAxisRaw("VerticalP1"));
         //moveInput = new Vector3(Input.GetAxisRaw("HorizontalP1"), 0, 0);
         moveVelocity = moveInput * moveSpeed;
         myRigidBody.velocity = moveVelocity;
         if (Input.GetKey(rotationLeft))
         {
             transform.Rotate(Vector3.up, -moveRotation * Time.deltaTime);
         }
 
         if (Input.GetKey(rotationRight))
         {
             transform.Rotate(Vector3.up, moveRotation * Time.deltaTime);
         }
     }


How can i make the axis rotate with the object??

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

3 Replies

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

Answer by Bunny83 · Sep 18, 2017 at 11:09 AM

The velocity of a rigidbody is always defined in worldspace as any physics simulation is done in world space. So the way you set your velocity is always in worldspace. There are several ways how you can solve this. The most simplest one is to first define your movement vector in local space. Your current moveInput can be treated as localspace. Then just transform this direction into worldspace by using transform.TransformDirection().

 // input in local space
 moveInput = new Vector3(Input.GetAxisRaw("HorizontalP1"), 0, Input.GetAxisRaw("VerticalP1"));
 
 // transform into worldspace:
 moveInput = transform.TranslateDirection(moveInput);
 
 moveVelocity = moveInput * moveSpeed;

Another way would be to compose the final move input by directly adding and scaling the appropriate worldspace vectors together:

 moveInput = Vector3.zero;
 moveInput += transform.right * Input.GetAxisRaw("HorizontalP1");
 moveInput += transform.forward * Input.GetAxisRaw("VerticalP1");
 moveVelocity = moveInput * moveSpeed;

Keep in mind that the way we handle the input at the moment we use a square "input field". So moving diagonally will make you move faster than just running straight forward. That's because the diagonal of out input square has a lenght of Sqrt(2) (~1.414..). There are other games out there which did not account for this.

One solution is to use "ClampMagnitude" to clamp the length of the input vector to "1":

 moveInput = Vector3.ClampMagnitude(moveInput, 1f);
 moveVelocity = moveInput * moveSpeed;

Clamp magnitude allows the vector to be shorter but not larger than the specified length. That way no matter which direction you run you always run at the specifed max speed and not faster.

Keep in mind that setting the velocity directly will cancel any velocity that might be applied by the physics system (like gravity). You may want to preserve the y-velocity.

 // ....
 moveVelocity.y = myRigidBody.velocity.y;
 myRigidBody.velocity = moveVelocity;
Comment
Add comment · Show 1 · 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 Formigaz · Sep 18, 2017 at 11:22 AM 0
Share

Thank you for your answer!! It really help me in my project and for future ones too.

Really appreciate the help Bunny83!! :D

avatar image
0

Answer by MT369MT · Sep 18, 2017 at 05:48 AM

Hi Try to use: moveInput = Vector3.forward;

Comment
Add comment · Show 1 · 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 Formigaz · Sep 18, 2017 at 10:42 AM 0
Share

Thank you for the answer $$anonymous$$T369$$anonymous$$T,

But i want to be able to controle the object(player), if i use the Vector3.forward it will always go forward.

avatar image
0

Answer by cgarossi · Sep 18, 2017 at 11:01 AM

Z is forward in world space.

So, to get the local forward of your object use:

 myRigidbody.velocity = transform.forward * moveSpeed;

transform.forward is your local forward axis.

Comment
Add comment · Show 1 · 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 Formigaz · Sep 18, 2017 at 11:14 AM 0
Share

Hello cgarossi, thanks for your answer,

but if i implement your code i won't be able to controle the player with w,a,s,d, it will always go forward.

The problem is when i rotate the player the axis don't rotate with him.

An example: if i turn my player 180 degrees the z axes didn't rotate and will have the same position, so if i press w for walk forward he will walk for the side.

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

100 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Build rotation tool for level editor 0 Answers

Rotating Cube with oculus touch controllers 0 Answers

Smoothly rotate object based on GetAxis 1 Answer

Rotating a bone upwards relative to camera. 2 Answers

Why is this rotation not performed as expected? 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