Physics required for collision detection?

So here is my scenario:

http://www.youtube.com/watch?v=W7eG28MBK6I

I would like the paddle to NOT be able to pass through the walls/bricks. I have tried variations/combinations of rigid bodies, triggers, etc, but did not get the desired effect (notably the paddle being affected by phsyics that I didn’t want).

I remember reading a snippet somewhere about using RayCasting, especially in the case of fast moving objects, so perhaps the better way to go is with that? If so, no idea where to start from xD

Code: PaddleScript.js

#pragma strict

//***********
//Variables
//***********
var Ball : GameObject;
var paddleSpeed : float = 10;
var codeBall : GameObject = null;
var inGameLives = 4;
//***********

function Start () {
	//Spawns the ball at the beginning of the games with the SpawnBall function
	SpawnBall();
}

function SpawnBall() {
	//Spawns ball
	codeBall = Instantiate(Ball, transform.position + Vector3(0,.475,0),Quaternion.identity);
	inGameLives = inGameLives - 1;
}

function Update () {

	//Moves paddle left/right/up/down
	transform.Translate(paddleSpeed * Time.deltaTime * Input.GetAxis("Horizontal"),paddleSpeed * Time.deltaTime * Input.GetAxis("Vertical"),0);

	//Keeps the paddle from moving below the terrain
	if (transform.position.y < -5.5){
		transform.position = Vector3(transform.position.x,-5.5, transform.position.z);
	}
	
	//Keeps the paddle from moving beyond the right wall. Needs to be fixed
	if (transform.position.x > 6.50) {
			transform.position = Vector3( 6.50, transform.position.y, transform.position.z);
		}
	//Keeps the paddle from moving beyond the left wall. Needs to be fixed	
	if (transform.position.x < -6.50) {
			transform.position = Vector3( -6.50, transform.position.y, transform.position.z);
		}
	
	//Launches the ball at the start of the game, as long as "codeBall" proves true first
	//Also moves the ball with the paddle prior to launching
	if (codeBall){
		codeBall.rigidbody.position = transform.position + Vector3(0,.475,0);
			if (Input.GetButton("Launch")) {
			codeBall.rigidbody.isKinematic = false;
			codeBall.rigidbody.AddForce(300 * Input.GetAxis("Horizontal"),300,0);
			codeBall = null;
		}
	}
	
	//Moves the camera along with the paddle on the Y Axis
	Camera.mainCamera.transform.position = Vector3(0,transform.position.y,-10);
}

function OnCollisionEnter(paddleHitSomething : Collision) {
	//Alters the direction of a ball when it hits the paddle based on where it hit the paddle
	if (paddleHitSomething.gameObject.tag == "Ball"){
		for (contact in paddleHitSomething.contacts){
			if (contact.thisCollider == collider) {
				var paddleHit = contact.point.x - transform.position.x;				
				contact.otherCollider.rigidbody.AddForce( 300f * paddleHit, 0, 0);
			}
		}
	}
}

Bonus help: Any idea on how to keep the ball better “attached” to the paddle?

And yes: I love to comment my code :smiley:

There are many ways to approach this. If you want to use Physics, since are for the game itself I presume…

Attach a box collider to the paddle and size appropriately. Attach a rigidbody and set it to kinematic.

Move your code in Update() over into FixedUpdate().

Keep track of the last valid position the paddel was in by storing this on a variable on the script. Update at the top of FixedUpdate().

When you get a collision on the paddle (that isn’t the ball), move the paddle back to the stored last valid position.

Your ball should also be better attached by using FixedUpdate() instead of Update().

The issue with my code was that I was setting the transform position instead of the rigidbody position, with transform performing no collision detection.

Besides the revised code for the paddle below, I also had to increase the “paddleSpeed” from 14.5 to 600, and had to have a non-kinematic rigid body on the paddle with Z Position + XYZ Rotation constraints. The ball is no longer staying exactly on top of the paddle, but I will fix that later. I also used velocity instead of MovePosition on the rigidbody, as there was some force being added to the paddle on MovePosition I didn’t like

Note: Earlier answer had me move everything to FixedUpdate for keeping the ball better attached.

New PaddleScript.js

#pragma strict

//***********
//Variables
//***********
var Ball : GameObject;
var paddleSpeed : float = 600;
var codeBall : GameObject = null;
var inGameLives = 4;
//***********

function Start () {
	//Spawns the ball at the beginning of the games with the SpawnBall function
	SpawnBall();
}

function SpawnBall() {
	//Spawns ball
	codeBall = Instantiate(Ball, transform.position + Vector3(0,.475,0),Quaternion.identity);
	inGameLives = inGameLives - 1;
}

function FixedUpdate () {

	//Moves paddle left/right/up/down
	rigidbody.velocity = Vector3(paddleSpeed * Time.deltaTime * Input.GetAxis("Horizontal"),paddleSpeed * Time.deltaTime * Input.GetAxis("Vertical"),0);

	//Launches the ball at the start of the game, as long as "codeBall" proves true first
	//Also moves the ball with the paddle prior to launching
	if (codeBall){
		codeBall.rigidbody.position = rigidbody.position + Vector3(0,.475,0);
			if (Input.GetButton("Launch")) {
			codeBall.rigidbody.isKinematic = false;
			codeBall.rigidbody.AddForce(300 * Input.GetAxis("Horizontal"),300,0);
			codeBall = null;
		}
	}
	
	//Moves the camera along with the paddle on the Y Axis
	Camera.mainCamera.transform.position = Vector3(0,transform.position.y,-10);
}

function OnCollisionEnter(paddleHitSomething : Collision) {

	//Alters the direction of a ball when it hits the paddle based on where it hit the paddle
	if (paddleHitSomething.gameObject.tag == "Ball"){
		for (contact in paddleHitSomething.contacts){
			if (contact.thisCollider == collider) {
				var paddleHit = contact.point.x - transform.position.x;				
				contact.otherCollider.rigidbody.AddForce( 100f * paddleHit, 0, 0);
			}
		}
	}
}