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
0
Question by hodge47 · Oct 19, 2015 at 11:38 PM · iosmobilerotate objectaccelerometer

Rotate object with accelerometer?

alt text Hello. I am working on a motocross game but I have been stuck for a really long time on rotating objects with the accelerometer. I have been putting it off and working on other things but it is now a vital part of me completing my game. In real life riding the bike acts like a gyro. When you give it gas it rotates backwards and opposite when you put on the brakes. I am wanting to do the same with the accelerometer but tilting the phone forward to lean forward and back to lean backward. I converted the steering to use the accelerometer but I have no clue how I did it because it has been so long ago.

I know that if(!grounded) I want to enable the tilt in either direction, forward or back. However I do now know whether to use transform.rotate() or rigidbody.moveRotation() and how I would implement that with the accelerometer.

 public Vector3 zeroAc;
     public Vector3 curAc;
     public float yTilt;
     public float sensH = 2f;
     public float sensV = 2f;
     //public float sensD = 2f;
     public float smooth = 0.4f;
     public float GetAxisH = 0.000f;
     public float GetAxisV = 0.000f;
     //public float GetAxisD = 0.000f;
 
     public bool hasInAirControl = false;

Here are my accelerometer variables. I am using sensH and GetAxisH for my steering(horizontal).

 if (!crash) {
 
                 steer = Mathf.MoveTowards (steer, GetAxisH/*Input.GetAxis("Horizontal")*/, 0.1f);
                 accel = GetAxisV;//Input.GetAxis("Vertical");
                 brake = Input.GetButton ("Jump");
                 shift = Input.GetKey (KeyCode.LeftShift) | Input.GetKey (KeyCode.RightShift);
             } else {
                 steer = 0;
             }
 
 
         } else if (controlMode == ControlMode.touch) {
 
             if (accelFwd != 0) {
                 accel = accelFwd;
             } else {
                 accel = accelBack;
             }
             steer = Mathf.MoveTowards (steer, steerAmount, 0.07f);
 
         }

and my steering setup...

     public void BikeSteer (/*float amount*/)
     {
 
         //accelerometer
         
         curAc = Vector3.Lerp (curAc, Input.acceleration - zeroAc, Time.deltaTime / smooth);
         GetAxisV = Mathf.Clamp (curAc.y * sensV, -1, 1);
         //Debug.Log("v" + GetAxisV);
         GetAxisH = Mathf.Clamp (curAc.x * sensH, -1, 1);
         //Debug.Log("h" + GetAxisH);
         // now use GetAxisV and GetAxisH instead of Input.GetAxis vertical and horizontal
         // If the horizontal and vertical directions are swapped, swap curAc.y and curAc.x
         // in the above equations. If some axis is going in the wrong direction, invert the
         // signal (use -curAc.x or -curAc.y)
         
         //////
 
         steerAmount = GetAxisH;
     }

and here is my steering function.

Like I said I dont really know how I converted button steering to accelerometer because I havent touched my code in a few months. Any help would be greatly appreciated!

screen-shot-2015-10-19-at-44711-pm.png (122.9 kB)
Comment
Add comment · Show 1
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 MadDevil · Oct 20, 2015 at 05:33 AM 0
Share

in the BikeSteer function, last line steeramount = GetAxisH, gives the horizontal value to accelerometer, by this you can move the bike in left-right direction.

similarly, if you use the "GetAxisV", it will give the vertical values of the accelerometer.

you can use it to change the acceleration and braking system of the bike.

0 Replies

· Add your reply
  • Sort: 

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Accelerometer - accounting for gravity for arbitrary device angle 1 Answer

Using the accelerometer to move a player as the real user walks 2 Answers

Rotate object to match phone's orientation 1 Answer

New Gyroscope Controls in Unity 4? 2 Answers

[Mobile AR] Model faces away from camera? 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