Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 uanmanarmy · Mar 15, 2014 at 05:32 PM · move an objectvector3 operationsvector3.slerpvector3.project

Rotate 2d object to facing direction

Hello Unity Community, I have some problems.

Im using accelerometer for moving my 2d sprite and the problem comes when i want to rotate my 2d sprite according to the device's accelerometer direction.

Now my code is rotating my character but not on the z axes, only on y and x

Do you know how can i do it to rotate only on z axes and to be facing to the direction

This is my code:

 void Update () 
     {

          Vector3 move = Vector3.zero ;
          move.x= Input.acceleration.x;
          move.z = Input.acceleration.y;
 
         transform.Translate(Vector3.right * (move.x * maxSpeed) * Time.deltaTime,Space.World);
         transform.Translate(Vector3.up * (move.z * maxSpeed) * Time.deltaTime);
 
     
         if (move != Vector3.zero)
         {
             transform.rotation = Quaternion.LookRotation(-move);
         }
 }

Any help will be appreciated !

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

Answer by robertbu · Mar 15, 2014 at 10:27 PM

I cannot verify your accelerometer calculations. If the vector is right, you can do this:

 Vector3 dir = -move;
 float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
 transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);

This code assumes the front of the 2D object is on the right. If the front is up, then you need to subtract 90 from the angle before passing it to AngleAxis().

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 uanmanarmy · Mar 16, 2014 at 09:10 AM 0
Share

The rotation its working but now im facing with another problem. On the x axis it is moving very well but it dont move on the y ax , to move it on the y i have to face my device on the y +x , something in the middle

avatar image uanmanarmy · Mar 16, 2014 at 09:39 AM 0
Share

Also when im facing a negative direction its moving on the opposite direction

avatar image uanmanarmy · Mar 16, 2014 at 09:42 AM 0
Share

Done, i fixed it i change both direction to Space.World

Thank you again!! :3

avatar image Liuk3 uanmanarmy · Jul 12, 2021 at 10:38 AM 0
Share

sorry, im probably not smart enough. what did you actually change to make it work?

avatar image Okabim · Sep 21, 2020 at 01:58 PM 0
Share

Thank you for this, 6 years in the future. This helped me tremendously.

avatar image GameCodeurStudent · Jan 24, 2021 at 08:49 PM 0
Share

yeah thx i already see this in black thorn udemy tutorial but forgot about it. Thx after 2 hrs of strugle. hello from begening 2021

avatar image Liuk3 GameCodeurStudent · Jul 12, 2021 at 10:28 AM 0
Share

im probably dumb, but what did you change from the code above to make it work?

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

23 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

Related Questions

Skips off block when landing 0 Answers

How can I get the position of Input.touches in World Space? 1 Answer

Slips off block when landing 0 Answers

Make Object smoothly follow player if out of near range 1 Answer

C# to Jacascript 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