• 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.slerpvector3 operationsvector3.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
3
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 3 · 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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

21 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

Related Questions

Skips off block when landing 0 Answers

Slips off block when landing 0 Answers

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

Codes are not working on my computer 0 Answers

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

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges