3D Pong Game onCollision Problem

Hello.

I am new to Unity and my first project is supposed to be some kind of a 3D Pong Prototype game for the IPhone. Thanks to the great documentation and the tutorials, I didn’t encounter that many problems yet.
Anyway, here is my setup, followed by my question:
There is a sphere = ball in a room and two rackets/bats.
To let the ball fly and bounce around in the room i am using two functions: FixedUpdate and OnCollisionEnter.

private var epsilon : float = 0.01;

function FixedUpdate() {
	if(isServed) {
		newPosition = direction.normalized;
		newPosition *= speed;
		newPosition *= Time.deltaTime;
		transform.Translate(newPosition);
//...
	}
}

function OnCollisionEnter(collision : Collision) {
	Debug.Log("Amount of collisions: " + collision.contacts.length);
//	for(var i : int = 0; i < collision.contacts.length; i++) {
		var contact = collision.contacts[0];
//...
		var normalOfObject = contact.normal;

		direction.x *= Mathf.Abs(Mathf.Abs(normalOfObject.x) - 1.0) < epsilon ? -1 : 1;
		direction.y *= Mathf.Abs(Mathf.Abs(normalOfObject.y) - 1.0) < epsilon ? -1 : 1;
		direction.z *= Mathf.Abs(Mathf.Abs(normalOfObject.z) - 1.0) < epsilon ? -1 : 1;
//	}
}

The ball itself has a sphere collider and a rigidbody attached(gravity is off, freeze rotation is on) (none of the other objects have rigidbody attached). The room is a rectangular tube (is that proper english?): A 3dObject imported from a 3ds max project and uses that mesh as its mesh collider. The two rackets/bats, that play the ball are rotated planes and use the plane as collider. This all works well, mostly.
But sometimes instead of bouncing off the wall, the ball sticks on the wall and stops moving completely. Or sometimes it stays in the corner and only moves along the z axis.
Is this bounce calculation somehow wrong? Mathf.Abs cannot take that long to calculate and produce this error, or am i wrong?

Also the ball sometimes just flies through the planes/rackets/bats, but i am guessing that this problem occurs because of the speed of the ball, since it happens far more often, whenever i set the speed to a high value (collision is not recognized?!). How could i get rid of that? Use a box instead of a plane?

Thanks for any help in advance!

(Oh how i love these “sometimes-happen-bugs”…)

Use box colliders instead of your mesh for border geometry, make them rather thick. Reason being, when your ball is moving fast it can very well pass right by a mesh collider in one frame and the collision therefore never register.

For your paddle, a flattened sphere primitive collider works wonders as it gives you built in ‘english.’

Scale matters much as well. Have your ball be 1 unity unit in size and scale everything else around that. Collision doesn’t work well at small scales due to floating point precision.

I’m a bit confused though, it looks like you are using a kinematic rigidbody and handling physics in script? Unity includes PhysX which can do everything for you, no code involved and it works perfectly for bouncing a ball about.

Crank up the max angular velocity setting in the physics settings, use a custom physics material with no friction and bounce set to one. Apply force or set an initial velocity vector on ball serve, sit back and enjoy.

Hey Quietus,

thanks for the quick reply! I am wondering how to apply 4 boxcolliders to my room object, since the editor only lets me apply a single object. Or do i build that object with boxes first? Here is my room:

BTW: This game is supposed to run on the iphone. Isn’t PhysX a lot to handle for the IPhone? The ball is not kinematic, because i needed the onCollisionEnter function. But i will give it a try and use those physic materials now.

EDIT: PhysX works great. Thanks. Even at extrem speed the mesh collider works fine!
EDIT2: How do i customize the sphere collider other than just adjusting the radius?

Regarding the four box colliders, you just make four separate objects.

Regarding customizing the sphere, what parameters (other than the radius) do you want to customize?

I would like to make an almost flat sphere.

I believe what you want is a cylinder. Flat sides. Create a cylinder primative, remove the capsule collider, add a mesh collider. It will default to the mesh of the cylinder. You should be good to go after that.