Simple Collision Deflection (Pong)

Hi,

I am working on my first Unity game, a Pong clone.

I have utilised some of the in built features to get the main game working although I am having trouble with the ball deflecting at an angle off the paddle unless it hits the edge. When it hits the flat face of the object it just deflects horizontally across the x plane.

The ball is a rigidbody which hits box colliders on the paddles. The ball has one script and the verticalcontroller script manages the paddles.

Also there is no gravity, angular drag or friction enabled.

using UnityEngine;
using System.Collections;

public class Ball : MonoBehaviour {

	private const float velocityIncrement = 1.0f;
	
	void Start() {
		
		Reset();	
	}
	
	void OnTriggerEnter(){
		Reset();
	}
	
	void Update()
	{		
		//rigidbody.AddForce(10, 10, 0, ForceMode.Impulse);
		rigidbody.velocity *= velocityIncrement;
	}
	
	private void Reset()
	{
		transform.position = Vector3.zero;
		rigidbody.velocity = new Vector3 (80,1,0);
		//rigidbody.angularVelocity = new Vector3 (0,10,0);
	}
		
	
}

using UnityEngine;
using System.Collections;

public class VerticalController : MonoBehaviour {

	public string axisName = "Vertical";
	public float speed = .5f;
	
	// Update is called once per frame
	void Update () {
	
		var delta = new Vector3(0,speed,0);
		
		if (Input.GetAxis(axisName) >= .001)
			delta *= 1f;
		else if (Input.GetAxis(axisName) <= -.001)
			delta *= -1f;
		else
			delta = Vector3.zero;
		
				 	 
		transform.position += delta;
	
	}
}

You should check out these functions:

http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.OnCollisionEnter.html
http://unity3d.com/support/documentation/ScriptReference/Collider.OnCollisionExit.html

Once you know your ball has collided with your paddle, you can add a force based on the balls distance to the center of the paddle. That way, if its above the center of the paddle it will make the ball go up, and if its below, it will make the ball go down.

It would look something like this

function OnCollisionEnter(collision : Collision) {
var ball:Rigidbody = collision.rigidbody;
if(ball.transform.y > transform.y){ ball.addforce(. . . . .) . . . . }
}