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 /
This question was closed Sep 23, 2013 at 03:00 AM by clunk47 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by RescuerRus · Sep 23, 2013 at 12:07 AM · inputupdatemoving

Rigidbody with Accelerometer

Hello community! I wanted to control a rigidbody by using accelerometer on android, i printed something like this:

 function Update() {
 if((Input.acceleration.x) > 0)
     {
         rigidbody.AddForce(Input.acceleration.x, 0, 0);
     }
 else if((Input.acceleration.x) < 0)
     {
         rigidbody.AddForce(Input.acceleration.x, 0, 0);
     }
 else
     {
         rigidbody.AddForce(Input.acceleration.x, 0, 0);
     }
 }

But nothing worked. So, i hope that Input.accelereation.x is integer, since i can't find any information about this function ^^.

Hope for a fast answer =) Thx

Look, imagine that rigidbody located at a big hill, then it will roll down, and while it is, you can move rigidbody with accelerometer, to the left and right. alt text

acceleration.png (30.9 kB)
Comment
Add comment · Show 13
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 meat5000 ♦ · Sep 23, 2013 at 12:14 AM 0
Share

Out of curiosity, try your code with Force$$anonymous$$ode.Impulse and see what happens.

avatar image clunk47 · Sep 23, 2013 at 12:15 AM 0
Share

You have the same exact statement in every condition. This means no matter what, you're adding force depending on the same axis used. First things first, clean this up.

 function Update()
 {
     rigidbody.AddForce(Input.acceleration.x, 0, 0);
 }

Then have a look HERE.

avatar image RescuerRus clunk47 · Sep 23, 2013 at 12:25 AM 0
Share

As i commented in answer Above, This code works strange: Rigidbody moves in 2 different directions.

avatar image clunk47 clunk47 · Sep 23, 2013 at 12:29 AM 0
Share

Of course it does. When you choose to move something on the axis depending on which why you're tilting, that's what happens. It's like hitting left or right keys... What exactly are you trying to do besides this? Please make your question more clear.

avatar image RescuerRus clunk47 · Sep 23, 2013 at 12:37 AM 0
Share

explained with picture in the answer ^^

Show more comments
avatar image meat5000 ♦ · Sep 23, 2013 at 01:02 AM 0
Share

Are you, erm. Are you trying to get this working in the editor? :P

If not I bet you are using a HTC

avatar image RescuerRus · Sep 23, 2013 at 01:04 AM 0
Share

What do you mean oO? In editor i can't find accelerometer. No, Xperia arc S

avatar image meat5000 ♦ · Sep 23, 2013 at 01:09 AM 0
Share

Just checking :P

Show more comments

2 Replies

  • Sort: 
avatar image
1
Best Answer

Answer by RescuerRus · Sep 23, 2013 at 01:33 AM

I finally fixed it. Just had to use RigidbodyConstraints to freeze the rotation of the rigidbody. It was rotating on different axes, which was causing it to move in the wrong direction.

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 clunk47 · Sep 23, 2013 at 02:59 AM 0
Share
  • for posting your solution. If any other answers or comments seemed to be helpful, please be so kind to vote them up as well. Happy Developing :)

avatar image meat5000 ♦ · Sep 23, 2013 at 07:47 AM 1
Share

...Never happens :P

avatar image clunk47 · Sep 23, 2013 at 04:57 PM 1
Share

There u go lol

avatar image
0

Answer by meat5000 · Sep 23, 2013 at 12:10 AM

Just try this in a script and see what it gives you.

 var dir : Vector3 = Vector3.zero;
     
     function Update ()
     {
         dir.x = -Input.acceleration.x;
         dir.y = Input.acceleration.y;
         dir.z = Input.acceleration.z;
     }
     
     function OnGUI()
     {
         GUI.Box (Rect (20,20,200,200),dir);
     }

You will find that x,y,z vary between -1 -> 0 -> 1 depending on the way the device is held. And, it's a Float

Comment
Add comment · Show 2 · 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 RescuerRus · Sep 23, 2013 at 12:23 AM 0
Share

I only needed to move rigidbody on x-axis, and btw, still nothing happend. This one worked fine http://docs.unity3d.com/Documentation/ScriptReference/Input-acceleration.html But it worked strange with rigidbody, they somehow conflicted by moving a body in different directions.

avatar image meat5000 ♦ · Sep 23, 2013 at 01:10 AM 0
Share

Did you look at the OnGUI output to see if your accelerometer is working? Also notice how x is -ve

Follow this Question

Answers Answers and Comments

17 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

Related Questions

input not working on high framerate 0 Answers

Input and applying physics, Update or FixedUpdate? 3 Answers

Why Do Inputs Have to Been in Update (Rather than FixedUpdate)? 3 Answers

Simple rigidbody.AddForce only applying once each time key is pressed 1 Answer

Keyboard Input Question 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