Code in OnCollisionEnter?

Attached is my game scene. The user can have maximum of 5 collisions with the obstacles. So I would like to know what must be the code when the ball hits the obstacle? I mean the code for continuing the game with ball moving forward(transform.translate).

Below code is currently present in OnCollisionEnter function in BallController program:

transform.position = new Vector3( transfomr.position.x, transform.position.y - 0.1f, transform.position.z)

transform.Translate (Time.deltaTime*2, 0, 0);

rest of the code is for other tasks like reducing the no of balls remaining etc.

This is working fine. But sometimes after collisions, the ball going in an entirely different direction & finally it exits the scene slowly. I don’t know what is the reason behind it. Am I missing something? This is the first time I am developing a code in Unity.

The problem is that you are not checking whether the collision was fired because you hit a top or a bottom block. You could for example:

  • set the tag of the topBlocks to “topBlock”

  • set the tag of the bottomBlocks to “bottomBlock”

    void OnTriggerEnter2D(Collider2D other) {
    if (other.tag == “topBlock”) {
    transform.position = new Vector3( transform.position.x, transform.position.y - 0.1f, transform.position.z);
    // & reduce lifes and so on
    } else if (other.tag == “bottomBlock”) {
    transform.position = new Vector3(transform.position.x, transform.position.y + 0.1f, transform.position.z);
    // & reduce lifes and so on

And the most important: post your code and post it formated right!