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 kkrulec · Apr 11, 2012 at 06:29 PM · javascriptrigidbodygravityaccelerometerjavascript-specific

Input.acceleration on sphere with rigidbody(gravity)

I am making a school project, where I controll an sphere with devices accelerometer. Now everithing goes well, till I add rigidbody to the sphere. Sphere dose move according to the information from accelerometer for few moments, but then it starts to get stuck. Like it would get stuck in the ground(cube in my case).

I am using this code for accelerometer:

  //move object using accelerometer
 
 var speed = 10.0;
 
 
 function Update () {
 
     var dir : Vector3 = Vector3.zero;
     
     dir.x = -Input.acceleration.y;
     dir.z = Input.acceleration.x;
     
     if(dir.sqrMagnitude > 1)
         dir.Normalize();
         
     
     dir *= Time.deltaTime;
     
     transform.Translate(dir * speed);
 
 }

How can I get this to work. Or is there a different way to apply movement to the object with information from accelerometer and have gravity.

I am developing with JavaScript for Android 2.2

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 pin70 · Nov 03, 2013 at 02:16 AM 0
Share

have you solved this issue? i have the same doubt.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by aldonaletto · Nov 03, 2013 at 02:25 AM

Translate by default acts in the local directions, what is a disaster for a sphere with a rigidbody: as the sphere rotates, the X and Y local directions at some moment will roughly point at the ground, making the sphere get stuck. Specify Space.World in Translate, and the world X and Y will be used instead:

   transform.Translate(dir * speed, Space.World);
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 pin70 · Nov 03, 2013 at 09:22 PM 0
Share

im moving a sphere with forces (you can see what i have done by following the Unity project 00 on how to make a moving sphere) if i place this translate i cant even see the sphere when entering the game because it probably flies always at any little movement in the device... if i put this rigidbody.AddForce(Input.acceleration speed Time.deltaTime); ins$$anonymous$$d of the translate it actually works kind off... because when the device is in the ground with the home button on the right the sphere goes back, for it to stop move i have to hold the device with the home button on the left and the screen facing back. sry if it`s hard to understand since english isn`t my native language.

avatar image aldonaletto · Nov 03, 2013 at 10:01 PM 1
Share

You have a totally different problem: the accelerometer axes aren't aligned to your game's axes. From your description, in your game the Z axis goes forth and back, but in the accelerometer the Z axis is perpendicular to the screen, pointing to the user! That's how the accelerometer axes are aligned to a typical device:

alt text

You should map them to the axes you want - probably the code below would work in your case:

 var force: float = 10.0;
 
 function Update () {
     var dir : Vector3 = Vector3.zero;
     dir.x = -Input.acceleration.y;
     dir.z = Input.acceleration.x;
     if (dir.sqr$$anonymous$$agnitude > 1) dir.Normalize();
     rigidbody.AddForce(dir * force);
 }

avatar image pin70 · Nov 04, 2013 at 01:43 AM 0
Share

Thank you sire it worked perfectly... all i had to do was change the x to = input acceleration.x and the Z to = input acceleration y. you sir deserve a medal

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Turning a gameobject's gravity on/off 1 Answer

Player Falls Over 1 Answer

Rigidbody Gravity scripting question (Javascript Answers Only!) 3 Answers

Add Explosion Force without torque? 1 Answer

How to set velocity of Rigidbody without changing gravity? 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