- Home /
Collision / Gravity Problem Not Updating and Pass through
Hi all, I'm having a problem with a simple collision / gravity setup. I have a sphere with a rigid body component so are using Unity Physics. Collision Detection is set to Continuous Dynamic and set to use gravity. I have made a basic enclosed area with cubes (like a simple maze, and sat the sphere inside). Each wall is a child of an empty game object simply called Maze (so I can reference them collectively). There is a box collider set on each of the walls. I have written a script which simply rotates the maze using the left and right arrow keys (this seems to work, see below).
//rotates the object
var speed = 50.0;
function FixedUpdate () {
var z = Input.GetAxis("Horizontal") * Time.deltaTime * speed;
transform.Rotate(0, 0, z);
}
This script is attached to the Maze object. When I hit play, the sphere drops appropriately and lands on the bottom wall (sphere has a z constraint so it doesn't roll off the side) . Then when I hit the left and right keys the maze is rotated correctly.
The problem I'm having is that the ball sometimes rolls along the rotated wall as it is supposed to, and other times it doesn't. Sometimes, after the sphere has initially hit and stopped, the maze can be rotated straight through the sphere. It seems to take a few seconds and then the sphere operates under gravity again, and falls as it should.
The things I have tried is increasing the mass of the sphere, changing the script from Update to FixedUpdate, changing the box colliders to mesh colliders, and changing the collision from Continuous Dynamic to Dynamic.
In the stats this scene is running at between 60 and 70 fps so doesn't seem to be a performance issue. I'm obviously doing something wrong. Any suggestions would be appreciated.
Best Regards Chris
Answer by em2 · Aug 10, 2011 at 11:57 PM
I think I've fixed it. The problem appears that the gravity on the sphere was going to sleep. I put the following script on the sphere, and although there is no actual force applied, it does keep the object forces 'awake'. As I'm new I would appreciated if anyone could let me know if there is a better solution, or if I have implemented it poorly.
//Script on the sphere
function FixedUpdate(){
rigidbody.AddForce(0,0,0);
}
Cheers Chris