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
1
Question by coolbird22 · May 18, 2014 at 08:58 AM · rotationaccelerometer

Code for rotating in direction of motion with the mouse not working when switched to accelerometer.

HERE is my previously answered question to achieve the rotation when the mouse is in question. I tried to convert the same code to control the object using the Accelerometer, but neither does the object move in the direction of the tilt, nor is it rotating in that direction. Here is my current code for the accelerometer.

 void Update(){    
         Vector3 dir = Input.acceleration;
         if (dir.sqrMagnitude > 1)
             dir.Normalize();
         dir *= Time.deltaTime;
         transform.Translate(dir * speed);
         float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
         transform.rotation = Quaternion.AngleAxis(angle+90.0f, Vector3.forward);    
 }

If anyone else has any suggestions for this, I request you to kindly add them. Thanks !

Comment
Add comment · Show 3
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 coolbird22 · May 18, 2014 at 03:07 PM 0
Share

@fafase, I've been after this issue for quite a few days and have tried a lot of solutions, some from other questions, forums and some my own. I also don't encourage the blatant copy-and-pasting nature that can easily become a part of a newcomers' (like me) Unity learning process. Every question that I've asked here has been asked only after I tried and gave my best before asking for help. If that is the reason you feel that I deserve a downvote, I wish I could do more to not deserve it. As for asking for robertbu to be the one to ask this question to specifically, as I explained that he may be the best person to answer this as he had helped in pretty much the exact question in the past. Further apologies if I may have harmed the integrity of this wonderful forum, though such was not my intention to do so.

avatar image robertbu · May 18, 2014 at 03:15 PM 0
Share

It is not prohibited to call out a particular person, but it bad form and you will get some down votes. In addition I cannot currently help you. It is been ages since I played with the acelerometer, and I'm away from by device built setup. If I cannot visualize the acelerometer outputs, I cannot write a solution. So I suggest you edit your title and allow @fafase (or others) to help you. @fafase gives solid solutions.

avatar image coolbird22 · May 18, 2014 at 03:33 PM 0
Share

@robertbu I have edited the title and the question itself as suggested by you. I may have committed a social goof up and hope the community here accepts my mistake. I request anyone with any pointers to help me out with my situation here. Thanks !

1 Reply

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

Answer by fafase · May 18, 2014 at 03:45 PM

I would think the problem comes from the fact that the accelerometer uses a different coordinate system

If your world is flat, then the y axis of the accelerometer is the forward (z) axis of your object.

Here is how we were doing for a game using the accelerometer to move a mouse toy object:

 void Update () {
     Vector3 planarVelocity;
     rigidbody.velocity = new Vector3 (Input.acceleration.x*speed,0,Input.acceleration.y*speed);
     planarVelocity = new Vector3(rigidbody.velocity.x, 0, rigidbody.velocity.z);

     if (planarVelocity.magnitude > 0.5f)
         transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(planarVelocity), rotSpeed*Time.deltaTime);
     }
 }

There are a few variables you need to create and define but else it is quite straightforward approach.

The acceleration is used to define the velocity of the rigidbody, this, compares to Translate will creates a movement that interacts with the environment while Transalte will get you through any collider.

Then planarVelocity is just a fancy word, our programmer found to describe the direction vector...Then it is just a basic Slerp of the transform rotation.

Comment
Add comment · Show 4 · 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 coolbird22 · May 18, 2014 at 04:03 PM 0
Share

Thanks for replying, fafase. Also, thanks for explaining the code. $$anonymous$$y project is a top down game with the object facing Up by default and also as the forward axis of the object. On using your code, I'm getting errors in Unity that say that there is no Rigidbody attached to the object or the script needs to check if the component is attached before using it. Oddly, I do have the Rigidbody component attached. Though I have the $$anonymous$$ass = 1 and Gravity Scale = 0. Having Gravity Scale = 9.8 makes the object fall down. Neither FixedAngle nor Is$$anonymous$$inematic is ticked on. Interpolate is set to None.

EDIT: I have a question. Since we are using the X-Y plane, why are you not using Input.acceleration.y*speed in the Y coordinate of the rigidbody.velocity vector ? Similarly, why have rigidbody.velocity.y = 0 and have rigidbody.velocity.z to have a value, when we basically have nothing to do with the Z depth ?

EDIT: I was getting that error because I was using Rigidbody2D. I changed it to to use Rigidbody and the code worked in Unity but the object was just travelling along the X axis and flipping in the Y axis. I made some edits to the code and the following is where I reached.

         Vector3 planarVelocity;
         rigidbody.velocity = new Vector3 (Input.acceleration.x*speed,Input.acceleration.y*speed,0);
         planarVelocity = new Vector3(rigidbody.velocity.x, rigidbody.velocity.y, rigidbody.velocity.z);
         
         if (planarVelocity.magnitude > 0.5f)
             transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(planarVelocity), rotSpeed*Time.deltaTime);
         transform.rotation = Quaternion.Euler(lockPos, lockPos, transform.rotation.eulerAngles.z);

I had to add the last line since I had to lock the rotations to 0 in the X and Y axis and only have Z rotations since they are perpendicular to the screen and don't skew as X and Y rotations do. But, the object still does not face the direction it is moving in. There are rotations happening but they are not in the direction the object moves.

avatar image fafase · May 18, 2014 at 05:52 PM 0
Share

Concerning the last line you can constrain the rigidbody via the inspector.

As for why the y axis in the z position, I am myself a little confused why our programmer went this way, though it worked when device is used with home button to the right.

Also, if you need more Input info, look there:

http://docs.unity3d.com/Documentation/$$anonymous$$anual/Input.html

avatar image coolbird22 · May 18, 2014 at 07:31 PM 0
Share

Constraining the X and Y rotations of the rigidbody via the inspector is making the object rotate in exactly those axes, which is very weird. I'll just stick to manually locking the rotation as it is in my previous comment. As for the rotation goes, the Input manual didn't help as I can't be sure what other workaround can work for this. Just so it is more clear, the kind of motion that I'm going for is pretty much like in this gameplay video - HERE

avatar image coolbird22 · May 18, 2014 at 08:35 PM 0
Share

Yay! I got it to work finally. I had to mix up your code and the previous code for it to face in the direction it is travelling in. I also managed to get the constraining to work on the X and Y axes after using this code so that is a win-win for us. Here is the working code:

         Vector3 planarVelocity;
         rigidbody.velocity = new Vector3 (Input.acceleration.x*speed,Input.acceleration.y*speed,0);
         planarVelocity = new Vector3(rigidbody.velocity.x, rigidbody.velocity.y, rigidbody.velocity.z);
 
         float angle = $$anonymous$$athf.Atan2(rigidbody.velocity.y, rigidbody.velocity.x) * $$anonymous$$athf.Rad2Deg;
         if (planarVelocity.magnitude > 0.5f)
             transform.rotation = Quaternion.AngleAxis(angle(add offset if needed here), Vector3.forward);

Thanks a lot for all your help, @fafase & @robertbu !

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

20 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

Related Questions

accelerometer Rotation limiting 1 Answer

Adding animation clip stopped my gameobject from rotating. 1 Answer

Manipulate an objects rotation 1 Answer

Sphere "skids" on turns without rotating 1 Answer

Smooth tilt accelerometer 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