Stick rigidbody to surface


I have a cube which has been stretched to resemble a table and some cubes on it. Everything has a rigidbody attached.
What im doing is rotating the “table” through its angular velocity hence making all the cubes on it slide from one side to the other.

However if I apply too much velocity the cubes on the table make a jump upwards as a result of the force applied. I dont want this, since I only want the cubes to slide from side to side and following the rotation of the table like they are “sticking” to it.

What would be the best way to put restrictions on upwards force?

That’s very weird: if you’re just rotating the table in the horizontal plane (around axis Y), no forces should be applied in the upwards direction.

Anyway, you can use rigidbody.constraints to disallow physical effects in the Y axis, like this:

function Start(){
  rigidbody.constraints = RigidbodyConstraints.FreezePositionY;

Place this code in the cube script - or, if the cubes have no script, just check Rigidbody/Constraints/Freeze Position/Y in the Inspector.

NOTE: Freezing position in Y will have a side effect: the cubes will not fall from the table - if sliding out of the table, they will just “float” in the air.

The problem is tricky because you want some of the effects of physical simulation (sliding due to gravity) but not others (objects being pushed up or falling down when the sides of the table are lowered or risen due to the rotation).

You may be better off with a hack where you actually keep the table still but simulate that it’s being rotated by doing these two things:

  • Rotate the camera by the opposite of what you would have rotated the ground with.
  • Rotate the global gravity vector by the opposite of what you would have rotated the ground with.

This will give the effect of the “whole world rotating” but gravity remaining constant (because the gravity and camera follow each other).

If you need certain objects to not be affected by this world rotation you could use multiple cameras and first render the objects that are unaffected by a camera that’s not rotated and then the affected objects by the camera that is rotated.

Some things in your description are unclear, but I think you could probably reach a solution with a Configurable Joint. You could start by setting YMotion to Locked and Angular XMotion and ZMotion to Locked as well. This will make the object only able to move along its own x local x and z axis and only rotate around its own y axis.

If it doesn’t follow the table correctly you could maybe fix up any errors with a script that forces the objects up axis to be the same as the table’s up axis or which forces the objects to be at a specific height relative to the table, or just give them a strong force that pushes them towards the table surface along the table’s own negative y axis.