Search code examples
unity-game-enginegame-physics

How to make Unity3d move objects on a table?


I am working on a tabletop game that user controls using accelerometer in the phone. It is pretty similar to Labyrinth on iOS.

The scene contains a tabletop created using planes and cubes, and a sphere as a ball. As it works in Labyrinth game, on tilting phone ball should drop to the tilted side, while the camera stays centered to table. I am trying to do similar thing where user tilts the phone, and objects on table move to tilted side.

Currently, I add the x and z component to Physics.gravity on tilt. Sadly, this change in gravity does not affect the ball which stays put on the table. Use gravity is selected for the ball, and it drop down from height to the tabletop initially and then comes to halt. After the initial drop, ball does not react to any gravity change.

I have also tried rotating the whole table, but using transform.rotate does not work either. The table rotates perfectly, alongwith the camera, but the ball stays put hanging in the air. So, currently I am out of my depth about the issue.

Is there any other way to allow tilt action registered, so that ball moves to the tilted direction? I cannot use addforce function, as there are multiple objects which need to react to tilt, and it will be difficult to keep track of that many addforce calls. How else can I get it working?


Solution

  • See this post for some related information.

    As for why your sphere is sticking to the table, can I assume your sphere has a rigidbody? If so, you need need to wake it up:

    if (rigidbody.IsSleeping())rigidbody.WakeUp();

    Ideally you would make that call only after detecting a change in orientation/gravity, not every frame.