• 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 coolbird22 · May 14, 2014 at 05:58 PM · accelerometerfacing

When adding code to make object face the direction it is moving, the accelerometer goes all wonky.

I want my object to face towards the direction it is moving, based on the accelerometer. The movement is working alright, but when I add the code to make it face the direction it is travelling towards, the accelerometer goes all wonky and the object doesn't go where it is supposed to, based on the tilt. For example, tilting the device towards me makes it go up instead of down and tilting away from me doesn't move the player much from where it is. What is happening and why ? Here is my code :

 Vector3 dir = getAccelerometer(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.eulerAngles = new Vector3(0,0, angle);
 
Comment
Add comment · Show 18
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 ThePunisher · May 14, 2014 at 06:05 PM 0
Share

Can you show me the original code, too? I'm assu$$anonymous$$g this is the modified code? I'd like to see both and compare.

avatar image coolbird22 · May 14, 2014 at 06:12 PM 0
Share

There is a accelerometer recalibration part and a movement clamping part that I did not add in the above code. Here is the entire code:

 void calibrateAccelerometer(){
         Vector3 wantedDeadZone = Input.acceleration;
         Quaternion rotateQuaternion = Quaternion.FromToRotation(new Vector3(0f, 0f, -1f), wantedDeadZone);
         //create identity matrix ... rotate our matrix to match up with down vec
         $$anonymous$$atrix4x4 matrix = $$anonymous$$atrix4x4.TRS(Vector3.zero, rotateQuaternion, new Vector3(1f, 1f, 1f));
         //get the inverse of the matrix
         this.calibration$$anonymous$$atrix = matrix.inverse;
         }
 Vector3 getAccelerometer(Vector3 accelerator){
         Vector3 accel = this.calibration$$anonymous$$atrix.$$anonymous$$ultiplyVector(accelerator);
         return accel;
         }
 
 void Start(){
         calibrateAccelerometer();
         }
 
 void Update(){    
         
         Vector3 dir = getAccelerometer(Input.acceleration);
         if (dir.sqr$$anonymous$$agnitude > 1)
             dir.Normalize();
         dir *= Time.deltaTime;
         transform.Translate(dir * speed);
 
 
         float angle = $$anonymous$$athf.Atan2(dir.y, dir.x) * $$anonymous$$athf.Rad2Deg;
         transform.eulerAngles = new Vector3(0,0, angle);
     Vector3 $$anonymous$$ScreenBounds = Camera.main.ScreenToWorldPoint(new Vector3(0, 0, 0));
         Vector3 maxScreenBounds = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, 0));
 
         transform.position = new Vector3($$anonymous$$athf.Clamp(transform.position.x, $$anonymous$$ScreenBounds.x + 1, maxScreenBounds.x - 1),$$anonymous$$athf.Clamp(transform.position.y, $$anonymous$$ScreenBounds.y + 1, maxScreenBounds.y - 3), 0);
 }
 }
 
avatar image ThePunisher · May 14, 2014 at 06:15 PM 0
Share

Cool, thanks. I'm taking a look at it right now.

avatar image ThePunisher · May 14, 2014 at 06:28 PM 0
Share

One of the things I can see that looks wrong/weird is that you are multiplying your direction vector by the delta time as if to apply a certain amount of the direction. I think what you would want to do is apply only a certain amount of speed based on the delta time.

In other words, it needs to look more like this:

 Vector3 dir = getAccelerometer(Input.acceleration);
        if (dir.sqr$$anonymous$$agnitude > 1)
          dir.Normalize();
        transform.Translate(dir * (speed * Time.deltaTime));
 
avatar image coolbird22 · May 14, 2014 at 06:34 PM 0
Share

Umm yeah, that makes sense actually. Just wondering if speed * Time.deltaTime need to be in brackets since it all is gonna get multiplied.

Show more comments

1 Reply

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

Answer by coolbird22 · May 19, 2014 at 03:39 PM

Got it to work finally.

   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 = Mathf.Atan2(rigidbody.velocity.y, rigidbody.velocity.x) * Mathf.Rad2Deg;
        if (planarVelocity.magnitude > 0.5f)
          transform.rotation = Quaternion.AngleAxis(angle(add offset if needed here), Vector3.forward);
Comment
Add comment · 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

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

GameObject dosen't move 2 Answers

can I know What side of my rigidbody is facing down? 1 Answer

Get the needed rotation to face other object 1 Answer

C# 2d Always Rotate Facing Gameobject 1 Answer

Match the tilted angle of phone to object. 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