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
1
Question by Kenneth Andersen · Dec 08, 2011 at 03:34 PM · c#androidaccelerationtilt

C# Move object by tilting on android device.

I got a platform with a Spher on it in a 3D space. What I want to do, is to make the sphere move freely up and down the X-Axis and the Z -Axis by tilting the phone. So when I tilt the phone to the righ the sphere rolls to the right, and when I tilt it to the left it rolls to the left and so on.

I can easily write a script that works with input from a keyboard on a PC, it would look something like this:

 float amountToMove = movementSpeed * Time.deltaTime;
 Vector3 movement = (Input.GetAxis("Horizontal") * -Vector3.left * amountToMove) + (Input.GetAxis("Vertical") * Vector3.forward * amountToMove);
 rigidbody.AddForce(movement, ForceMode.Force);

It's the functionality of the above script, I want by tilting my phone. At the moment I'm using the script below, from unity script Ref. But I can't make it work as intended. The sphere get stuck and won't roll in the direction I want it to and it starts to bounch randomly.

 Vector3 dir = 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);

help with this script would really be appreciated. 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

5 Replies

· Add your reply
  • Sort: 
avatar image
6

Answer by RonanC · Jan 28, 2015 at 06:35 AM

This piece of code works a treat.

   void FixedUpdate(){
         Vector3 movement = new Vector3 (Input.acceleration.x, 0.0f, 0.0f);
         rigidbody.velocity = movement * speed;
     }

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
avatar image
2

Answer by $$anonymous$$ · Oct 02, 2012 at 10:51 AM

You can get the tilt information of the phone from the accelerometer. Check http://docs.unity3d.com/Documentation/ScriptReference/Input-acceleration.html

You have to take the orientation of your phone in account though. http://docs.unity3d.com/Documentation/ScriptReference/Input-deviceOrientation.html

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
avatar image
0

Answer by Musabbir · Jul 12, 2018 at 05:09 AM

 float playerMovingSpeed  = 20f;
 
 void FixedUpdate()
     {
         transform.Translate(Input.acceleration.x * playerMovingSpeed * Time.deltaTime, 
                             Input.acceleration.y * playerMovingSpeed * Time.deltaTime, 0);
     }
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
avatar image
0

Answer by tomcarnevale · Dec 08, 2020 at 04:23 PM

Was trying to solve this today and found a solution I like a lot better.

if you are working in a situation where all of your rigid bodies need to obey the tilt or you only have one rigidbody, you can (and maybe should) utilize unity's physics engine as much as you can. For me, i was struggling to create a decent "bounce" effect with a rigidbody on a "rail", where it would hit the edges of the screen and bounce like a ball. But, I was failing in my physics calculations and it was getting more and more complex. So I thought "why work this hard to emulate gravity when unity already does this?"

Be sure to freeze any rotations or positions on your rigidbody to fit your needs. For me, I froze everything but the Z axis, and my camera is top down. for me, Forward was positive Z. So I set my gravity's Z value equal to the phone's acceleration Y value. You will need to adjust how the X Y and Z of the Input.acceleration maps to the X Y and Z values of Physics.gravity.

So for me, I simply set gravity equal to the device acceleration, which is basically what we want

   public Vector3 fakeGravity;
 
   private void FixedUpdate()
     {
 #if !UNITY_EDITOR
        //You may need to adjust how the X, Y, and Z values of each vector map to eachother
         Physics.gravity = Input.acceleration;
 #elif UNITY_EDITOR
         //use this to test in editor
         Physics.gravity = fakeGravity;
 #endif
     }
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
avatar image
0

Answer by Llama_w_2Ls · Dec 08, 2020 at 04:51 PM

One problem with using an accelerometer, is that it exponentially increases in value the more you tilt. It's not an accurate way of measuring the angle of tilt (that would be gyroscope), but it would be good in a car-type-kind of scenario where, you might want to take hard rights when tilting further than normal.

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.

Follow this Question

Answers Answers and Comments

8 People are following this question.

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

Related Questions

C# Tilting object with acceleration on andoid 0 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Ball maze android issues 1 Answer

GameObject dosen't move 2 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