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
0
Question by Sajjoo · May 11, 2011 at 07:27 AM · rotationtiltunityiphonebike

rotating/tilting the bike through accelerometer in unity.

hello guys, i am working on bike racing game prototype for iphone. and i am stuck in turning and tilting the bike. here is my code with this code it tilts right to left but its not working smoothly and bike falls down. kindle give me some suggestion or hints to resolve this issue.

void Update () {

     Vector3 accelerator = Input.acceleration;

     float z = accelerator.z * -1; 
     //transform.Translate(Vector3.forward * z * 5); 

     rigidbody.AddRelativeForce(Vector3.forward * z * 100);

      float y = accelerator.y;  
      //transform.Rotate(-(Vector3.up * y * 10));

     rigidbody.AddRelativeTorque(Vector3.up * accelerator.y * 200);

}

thanks in advance

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
0

Answer by Cyb3rManiak · May 11, 2011 at 08:33 AM

The problem with this approach is that every frame the accelerometer reports a non-zero acceleration, you ADD force and torque. This means that if you hold the device at a constant angle, force (and/or torque) will be added and won't stop being added until finally it knocks down the bike.

If you want to use this approach, you'll probably need to limit how much force you can add - clamp it.

something like

Vector3 v3MaxAngularVelocity = new Vector3(3f, 3f, 3f);

if (rigidbody.angularVelocity.y <= v3MaxAngularVelocity.y) rigidbody.AddRelativeTorque(Vector3.up accelerator.y 200);

Don't take the axis or the value I've put in as an example, just as a clarification about what you need to do... Only add force/torque if your bike haven't reached the maximum velocity on that particular axis.

Another option is to give the bike some force/torque that will make it try and stand straight up each frame. Think about it - in real life, when the bike reaches a certain speed - you can take the hands from the handlebars. The acceleration of the bike forward will straighten it up. If you try and turn - you'll have some resistance. This way, each frame your accelerometer reports some angle - you add the force/torque just like you did in your example, and right after that add a reverse force that will straighten the bike. If the force you add to the bike based from the angle the accelerometer reports is stronger than the force/torque that makes the bike return to the original position - the bike will tilt.

Is it really necessary for you to use physics? If you want an arcade style bike control, it will be much easier for you to "fake" physics. You'll have much more control over it.

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 Sajjoo · May 11, 2011 at 09:06 AM 0
Share

thanks for your answer cybermaniac... but i want to know something that can i use Objective C for rotating and moving the bike. i think it will be more easy then java script. and if we can control accelerometer with objective c. can you please tell me how can i call Objc,s code into Unity?.

avatar image Cyb3rManiak · May 11, 2011 at 10:35 AM 0
Share

If that's indeed what you want (And I'm not sure exactly why) - you'll need to either use plugins written in Objective C (and you'll need the iPhone Adavanced version of Unity for that), or use the PlayerPrefs workaround. Check http://www.tinytimgames.com/2010/01/10/the-unityobjective-c-divide/ out if you indeed want to get into this. This was discussed a lot in the forum, so just search for "Objective C" and "PlayerPrefs".

avatar image Sajjoo · May 11, 2011 at 11:50 AM 0
Share

as you said about "Fake physics" in last two lines of your answer?. can you please give me some suggestions or hints how can i achieve that?

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

No one has followed this question yet.

Related Questions

Motorcycle/Bike Physics Trouble 5 Answers

Smooth Accelerometer tilt rotation 0 Answers

Tilt with rotation. 0 Answers

AddTorque behaving different in Unity 4.6 0 Answers

Rotate the bike,s wheels to move the bike. 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