# How to add collision detection between two game objects?

Hello,

I am remaking Pong as an practice exercise and right now I have the paddles moving correctly and the ball is also moving using the physics engine. However, I need a force to be added to the ball when it collides with one of the paddles. Right now the code that I have for the OnCollisionEnter2D method is adding to the velocity but it adds so much that the ball moves too fast and it stops colliding with the paddle. Would I need to use rigidbody2D.AddForce and not rigidbody2D.velocity?

using UnityEngine;
using System.Collections;

public class Ball : MonoBehaviour {

``````float ballSpeed = 50;
float timeWait = 0.3f;
float multiplier = .5f;

// Use this for initialization
IEnumerator Start () {
yield return new WaitForSeconds (timeWait);
GoBall ();
}

void OnCollisionEnter2D(Collision2D collision){
if (collision.collider.tag == "PlayerOffense") {
rigidbody2D.velocity += rigidbody2D.velocity * multiplier;
}

}

IEnumerator ResetBall () {
rigidbody2D.velocity = new Vector2 (0, 0);
transform.position = new Vector2 (0, 0);
yield return new WaitForSeconds (1);

GoBall ();
}
``````

void GoBall() {

``````float randomNumber = Random.Range (0, 2);
if (randomNumber <= 0.5) {
} else
``````

{
}
}
}

There’s a couple of ways you could do this. The first one that comes to mind is to simply negate the X velocity (assuming X is left-right) when it collides with either paddle. Something like this:

``````void OnCollisionEnter2D(Collision2D collision){
if (collision.collider.tag == "PlayerOffense") {
rigidbody2D.velocity.X = -rigidbody2D.velocity.X;
}

}
``````

The other way I can think of doing would be to physically simulate the paddles and ball, so you don’t manually change any velocities or modify forces, it just bounces. However, you would have to also set up a physics material for either the ball or the paddles to make it bouncy and create physical walls at the edges of the screen. To stop the ball just falling away, you would freeze movement on the Y axis and turn off gravity. Probably easier and more controllable to use the first method.

EDIT:

Ah, there’s a problem with the code I posted above. What’s happening is that when the ball collides with the paddle, it slightly moves inside the paddle. Its speed is reversed, but before it leaves the paddle, the next frame comes around and because it’s still inside the paddle, its speed is reversed again. This basically makes the ball stick to the paddle.

SO. What I now recommend is that you go back to using your old code, where you just had a bouncy material on the paddles and the ball was physically simulated. If you find the ball falls downwards while it’s moving, make sure to disable gravity in its properties. Then, to make sure it never slows down, in Update(), do something like this (bearing in mind I don’t know C#, only JS):

``````if(Mathf.Abs(rigidbody2D.velocity.X)<minspeed)
{
rigidbody2D.velocity=new Vector2(minspeed,rigidbody2D.velocity.Y);
}
``````

If you have any more questions feel free to comment and I’ll do my best to explain further

Hello, Clonkex. Thank you for answering.

I am making the game just like the original so the X is the left-right. I actually already added the bouncy material to the ball but after so long the ball loses its force/velocity and falls to the bottom. I am going to work with the solution you gave me and get back to you.

Thank you again