- Home /
Ensuring completely smooth movement along a series of box colliders
I am making a puzzle game which involves objects moving around a level influenced by gravity.
Sometimes an object will fall 'freely', IE, in mid air. Other times, an object will fall whilst brushing a flat surface comprised of a series of box colliders that are perfectly aligned with each other.
The issue is that objects that are brushing along the box colliders will fall slightly slower and sometimes they will bounce away from the surface at the point where two box colliders meet.
Here's an image more clearly demonstrating what the issue is.
http://dl.dropbox.com/u/1008157/Indie/physics-fig1.png
Obviously this is very annoying and isn't what I want.
There is 0 friction on each surface, there is 0 friction set in the physics material, both objects are completely identical in mass, drag, and the box colliders are perfectly aligned with each other.
I don't wish to use a single box collider that covers the entire surface as I would really prefer to stick with my modular approach of level design which makes propogating changes across multiple levels incredibly easy.
So my question is how can I solve this problem?
This diagram deserves my vote. Why don't you insert the image in the text?
Answer by TheDarkVoid · Aug 03, 2011 at 09:36 PM
the problem is the the physics in unity is not perfect and the falling box may be too close to the wall try offsetting it by about .01, you can also select freeze rotation in the rigid body options
Yes, I think freezing rotation may help. The problem probably is caused by the abrupt change in normals at the edges - physics simulation must calculate the contact points and normals, and at the edges this probably gives crazy results.
Ah yes. Offsetting did the trick! Here's the method I wrote to fix up the position.
float rayDistance = 4f; // Distance to raycast
float moveDistance = 0.5f; // Distance to translate object
// Raycast in 4 directions to check if we're hitting something
// Up
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.up), rayDistance)) transform.Translate(new Vector3(0, -moveDistance, 0));
// Right
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.right), rayDistance)) transform.Translate(new Vector3(-moveDistance, 0, 0));
// Down
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.down), rayDistance)) transform.Translate(new Vector3(0, moveDistance, 0));
// Left
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.left), rayDistance)) transform.Translate(new Vector3(moveDistance, 0, 0));
Answer by Instability · Jun 10, 2013 at 11:24 AM
Alternatively, you could create an empty gameobject and attach a box collider for the ground to it. You could use collision layers to make sure the ground collider does not interfere with the rest of the geometry, only with the player. Set the ground collider a tiny bit above the (uneven) floor geometry and it should roll smoothly. You can make the ground collider stretch large patches and it will be a continuous object.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Similar movement to a game (look description) 1 Answer
Arrow trajectory 0 Answers
Need a simple car physics script working on unity 5? 1 Answer