- Home /
Stick rigidbody to surface
Hi
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?
Some things are unclear here. Are you only rotating the table around the Y axis so it's always perfectly horizontal or do you rotate around other axes too?
Its only rotating around the Z axis so its tilting from side to side, hence making the objects slide from side to side
Answer by aldonaletto · Dec 25, 2011 at 11:06 PM
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.
This isn't my question topic, but I'm curious now :
There's no way to constrain only one direction of it's x/y/z axis? Like, freezing it's upward movement on Y to where it can never go up, but IS allowed to go down?
Edit: (if not, that would be a nice upgrade to unity3.6 : constraining axis -/+ only OR both) ...I also guess you could code it in, but the auto-constraint would still be a nice addition.
No, rigidbody constraints only affect the axes, not a particular direction. As you said, this can be done by script, clamping any positive rigidbody.velocity.y to zero, but not using constraints.
The thing is if im freesing the y position, the cube does not follow the rotation of the table. What I want to acheive is a sticking like effect so it looks like the cubes are always on the table following its rotation. This can be acheived by making them a child of the table, however im not sure if this is the best solution.
Ok, you don't want the objects to slide - they must stay in position when the table rotates. The best way is to child them to the table - but since the table has different scales in x, y and z, this may distort the cubes if they are moved or rotated. You can avoid this by creating an empty object that will be the table (move the rotation script to it) and child to this empty object the table model. Place the cubes over the table and child them to the empty object - the empty object is scaled 1,1,1, thus this will not affect the children.
Answer by runevision · Jan 10, 2012 at 11:12 PM
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.
Thanks I will try to work with this solution. However I noticed that when rotating the camera there are still some shakes. Since the camera is following the player the worst shakes happen when the player goes up or down and the view is rotating at the same time.
As I mentioned before the shakes are worst when im controlling the rotation through network. Im thinking that the camera probably does not follow the player smoothly due to lag. However I tried several solution to get this smoothly including Lerp.
Also when sending through network I tried RPC and script sync. Currently im syncing a script which sends the rotation amount from an iPhones accelerometer and using unreliable state sync. Putting the send rate up to a very high amount. Would there be a "best way" to make this smoothly or is some lag unavoidable. I noticed that when using the "Unity Remote" it runs a lot more smooth eventhough its still through network. I have an existing thread with this issue:
http://answers.unity3d.com/questions/202362/smooth-network-communication-like-unity-remote.html
I just came up with a solution which I think works well, I posted it in the link above. Seems it removes most of the lag when rotating the camera. Thanks a lot for your help! :)
Answer by runevision · Jan 10, 2012 at 02:30 PM
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.
Your answer
Follow this Question
Related Questions
Rigidbody y velocity is stuck on 0, gravity is not turned off. 2 Answers
Constraint not totally working after manually overriding a joint rotation 0 Answers
What is the velocity of Gravity? 1 Answer
Fixed Joint Rigidbody Colliding 1 Answer
How can I accelerate a rigidbody towards another at X m/s? 0 Answers