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
1
Question by PKMPGHPvdvP · Sep 12, 2017 at 07:45 PM · rotationmovementrotate objectmovinggameobjectmovements

Character Movement won't work properly because of the rotation

So here is my current code :

     void Update()
     {
         
         if (myX != leftController.GetTouchPosition.x || myY != leftController.GetTouchPosition.y) { //checks if player changed position.
             myX = leftController.GetTouchPosition.x;
             myY = leftController.GetTouchPosition.y;
 
             double rad = Mathf.Atan2(leftController.GetTouchPosition.y, leftController.GetTouchPosition.x); // In radians
             double deg = rad * (180 / System.Math.PI); // values from up right to up left : +0 to +180 and from down left to down right: -180 to -0
 
             double myRotAngle = 0;
             //calculating the specific angle in which the character should move
             if (deg < 0) {
                 myRotAngle = 90 + ((deg * -1));
             } else if (deg >= 0 && deg <= 90)
                 myRotAngle = 90 - deg;
             else if (deg >= 90 && deg <= 180)
                 myRotAngle = 270 + (180 - deg);
 
             transform.localEulerAngles = new Vector3 (0f,(float)myRotAngle, 0f);//rotate
         
         }
 
         _rigidbody.MovePosition(transform.position + (transform.forward * leftController.GetTouchPosition.y * Time.deltaTime * speedMovements) +
             (transform.right * leftController.GetTouchPosition.x * Time.deltaTime * speedMovements) );//move
     }

So basically the problem is that when I want to rotate the character and move it the movement wont work properly as expected. for example if I move my joystick down left my character moves up middle.

But If I only move the character without rotating him the character movement behaves as it should.

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
1

Answer by Vicarian · Sep 12, 2017 at 08:53 PM

You're applying the rotation to the character, but when you translate, you're not respecting that rotation - instead going with the raw values off the controller. This makes transform.forward and transform.right constantly move along 0, 0, 1 and 1, 0, 0 respectively. In other words, your forward and right vectors never update their orientation with respect to your rotation. To fix this, use the rotation of the transform instead of your raw controller values in your translation statement, and you should be good to go. Keeping in mind you're working with Vectors, you may simplify the statement:

 // transform.right + transform.forward simplifies to (1, 0, 1) 
 // This is the axis needed by the Quaternion driving the rotation, to test visually:
 //    Create a cube in Unity
 //    Set its position to 1, 0, 1
 //    Rotate some degrees in Y
 //    Drag the blue translation gizmo (the forward axis in Unity)
 //    You will see that only the X and Z positions change, 
 //    and it moves strictly along a ray pointed to by the blue arrow, 
 //    which I think is the intended behaviour.
 //        If you need to add controller input to control both axes, 
 //        simply scale by the controller value:
 //            (transform.forward * RawYAxisValue + transform.right * RawXAxisValue)

 _rigidbody.MovePosition(transform.localRotation * transform.localPosition + (transform.right + transform.forward) * Time.deltaTime * speedMovements);
Comment
Add comment · Show 29 · 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 PKMPGHPvdvP · Sep 12, 2017 at 09:56 PM 0
Share

Vicarian I get the error that I can't use '*' when dealing with vector3 or quaternion

avatar image Vicarian PKMPGHPvdvP · Sep 12, 2017 at 10:49 PM 0
Share

The order matters, I got it backwards. It's:

 _rigidbody.$$anonymous$$ovePosition(transform.localRotation * transform.localPosition + (transform.right + transform.forward) * Time.deltaTime * speed$$anonymous$$ovements);

Apologies, I can never remember the order unless I get that error.

avatar image PKMPGHPvdvP Vicarian · Sep 13, 2017 at 04:46 PM 0
Share

Vicarian I don't get the error anymore but it still wont work. When I try to move everything around the character rotates 360° all the time..... When I try to go for example to the right side the character moves a little and than stops and when it stops everything starts rotating

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

119 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 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

gameobject stops moving correctly when rotating 1 Answer

Determining the rotation of an object based on its upcoming position? 1 Answer

How can you make a 2D object move based on rotation, and rotate while moving? 1 Answer

how to rotate a gameobject in a given direction and stop when it's done 1 Answer

Rotacionar objeto 0 Answers


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