2D Breakout

This examples based on 2D Breakout Example Project.

This example was made by using CSharpParser to parse the following script:

  • Ball_script.cs
  • Brick_script.cs
  • GameManager_script.cs
  • GameOverDetector_script.cs
  • Paddle_script.cs

Original Script:

/// <summary>
/// 2D BREAKOUT Example Project
/// By Danar Kayfi 
/// Twitter: @DanarKayfi 
/// Personal Blog: https://danarkayfi.wordpress.com
/// Games I developed: https://bug-games.com
/// 
/// Special Thanks to Kenney for the amazing CC0 Graphic Assets: http://kenney.nl
/// 
/// License: (CC0 1.0 Universal) https://creativecommons.org/publicdomain/zero/1.0/
/// You're free to use these game assets in any project, 
/// personal or commercial. There's no need to ask permission before using these. 
/// Giving attribution is not required, but is greatly appreciated
/// </summary>

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Ball_script : MonoBehaviour 
{
  //Public Variables will appear in the Inspector 
    public float ballInitialVelocity = 600f;	//The initial velocity of the Ball
    public Text txtScore;	//Connected to the Score text
    public AudioClip sfxBrickHit;	//Connected to SFX of the Brick Hit
  public AudioClip sfxPaddleBorderHit;	//Connected to SFX of the Paddle Border Hit

  //Private Variables
    private Rigidbody2D rb2D;	//Will Connect with the Ball Rigidbody 2D Component
  private AudioSource audioSource; //Will Connect with the Ball Audio Source Component
  private bool ballInPlay;	//Boolean to know if the Ball inPlay (in moving) or not
    private int totalScore;		//Counter for the total Score


  // Use this for initialization
  void Start () 
    {
    rb2D = GetComponent<Rigidbody2D>();		//Connect rb2D with the Ball Rigidbody 2D Component
    audioSource = GetComponent<AudioSource>();		//Connect rb2D with the Ball Audio Source Component
  }
  
  // Update is called once per frame
  void Update () 
    {
    //If The player pressed Space button & The ball is not inPlay (in moving)  & The game can start
        if (Input.GetButtonDown("Jump") && ballInPlay == false && GameManager_script.startGame)
        {
            transform.parent = null;	//Disconnect the Ball from the Paddle
      ballInPlay = true;		//The Ball is inPlay (is moving)
      rb2D.isKinematic = false;		//Uncheck the isKinematic in the Rigidbody 2D

            if(Input.GetAxis ("Horizontal") == 0f)		//Checking if the Paddle is standing still
        rb2D.AddForce(new Vector2(1f, ballInitialVelocity));	//Adding force to the Balle
      else if(Input.GetAxis ("Horizontal") > 0f)		//Checking if the Paddle is moving right
        rb2D.AddForce(new Vector2(ballInitialVelocity, ballInitialVelocity));		//Adding force to the Balle
      else if(Input.GetAxis ("Horizontal") < 0f)		//Checking if the Paddle is moving left
        rb2D.AddForce(new Vector2(-ballInitialVelocity, ballInitialVelocity));		//Adding force to the Balle
        }


  }

  //Will be called when a Collision Enter accure 
    void OnCollisionEnter2D(Collision2D coll)
    {
    //If a collision caused by a Brick 
        if (coll.gameObject.tag == "Brick")
        {
            audioSource.clip = sfxBrickHit;		//Load Brick Hit SFX to the Audio Source component
      audioSource.Play();		//Play Brick Hit SFX
            totalScore += coll.gameObject.GetComponent<Brick_script>().score;		//Add this Brick score to the total score 
            txtScore.text = totalScore.ToString("000000");		//Show the total score as a Text containing 6 digits
            Destroy(coll.gameObject);		//Destroy the brick
        }

    //If a collision caused by a Paddle or a  Border
        if (coll.gameObject.tag == "Paddle" || coll.gameObject.tag == "Border")
        {
      audioSource.clip = sfxPaddleBorderHit;		//Load Paddle/Border Hit SFX to the Audio Source component
      audioSource.Play();		//Play Brick Hit SFX
        }
    }
}
/// <summary>
/// 2D BREAKOUT Example Project
/// By Danar Kayfi 
/// Twitter: @DanarKayfi 
/// Personal Blog: https://danarkayfi.wordpress.com
/// Games I developed: https://bug-games.com
/// 
/// Special Thanks to Kenney for the amazing CC0 Graphic Assets: http://kenney.nl
/// 
/// License: (CC0 1.0 Universal) https://creativecommons.org/publicdomain/zero/1.0/
/// You're free to use these game assets in any project, 
/// personal or commercial. There's no need to ask permission before using these. 
/// Giving attribution is not required, but is greatly appreciated
/// </summary>

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class GameManager_script : MonoBehaviour 
{
  //Public Variables will appear in the Inspector 
  public GameObject mainMenuPanel;	//Connected to the Main Menu Panel
  public GameObject gameOverPanel;	//Connected to the Game Over Panel
  public static bool startGame = false;	//Boolean to know if the game started or not
  public static bool gameOver = false;	//Boolean to know if the game is over or not

  // Update is called once per frame
  void Update () 
    {
    //If The player pressed Space button & The game didnt start yet
        if (Input.GetButtonDown("Jump") && startGame == false)
        {
            startGame = true;		//Turn Start Game boolean to true
            mainMenuPanel.SetActive(false);		//Turn off Main Menu Panel
        }

        if (Input.GetButtonDown("Jump") && gameOver == true)
        {
      startGame = false;		//Turn Start Game boolean to false
      gameOver = false;		//Turn Game Over boolean to false
            SceneManager.LoadScene("MainGame");		//Load Main Game Scene
        }
  }
}
/// <summary>
/// 2D BREAKOUT Example Project
/// By Danar Kayfi 
/// Twitter: @DanarKayfi 
/// Personal Blog: https://danarkayfi.wordpress.com
/// Games I developed: https://bug-games.com
/// 
/// Special Thanks to Kenney for the amazing CC0 Graphic Assets: http://kenney.nl
/// 
/// License: (CC0 1.0 Universal) https://creativecommons.org/publicdomain/zero/1.0/
/// You're free to use these game assets in any project, 
/// personal or commercial. There's no need to ask permission before using these. 
/// Giving attribution is not required, but is greatly appreciated
/// </summary>

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameOverDetector_script : MonoBehaviour 
{
  //Public Variables will appear in the Inspector 
  public GameObject gameOverPanel;	//Connected to Game Over Panel

  //Will be called when a Collision Enter accur
    void OnCollisionEnter2D(Collision2D coll)
    {
    //If a collision caused by the Ball
        if (coll.gameObject.tag == "Ball")
        {
            Destroy(coll.gameObject);		//Destroy the ball
      GameManager_script.gameOver = true;		//Turn Game Over boolean to true
      gameOverPanel.SetActive(true);		//Turn on the Game over Panel
        }
    }
}
/// <summary>
/// 2D BREAKOUT Example Project
/// By Danar Kayfi 
/// Twitter: @DanarKayfi 
/// Personal Blog: https://danarkayfi.wordpress.com
/// Games I developed: https://bug-games.com
/// 
/// Special Thanks to Kenney for the amazing CC0 Graphic Assets: http://kenney.nl
/// 
/// License: (CC0 1.0 Universal) https://creativecommons.org/publicdomain/zero/1.0/
/// You're free to use these game assets in any project, 
/// personal or commercial. There's no need to ask permission before using these. 
/// Giving attribution is not required, but is greatly appreciated
/// </summary>

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[System.Serializable]	//This class will appear in the Inspector
public class Boundary
{
  //Public Variables will appear in the Inspector
  public float xMin, xMax; //Screen Boundary dimentions (minimum x axis & maximum x axis)
}

public class Paddle_script : MonoBehaviour 
{
  //Public Variables will appear in the Inspector
    public Boundary boundary;		//Create an object from class
    public float paddleSpeed;		//Speed of the paddle

  //Private Variables
  private Rigidbody2D rb2D;		//Will Connect with the Ball Rigidbody 2D Component

  // Use this for initialization
  void Start () 
    {
    rb2D = GetComponent<Rigidbody2D>();		//Connect rb2D with the Ball Rigidbody 2D Component
  }
  
  // Update is called once per frame
  void Update () 
    {
    float moveHorizontal = Input.GetAxis ("Horizontal");	//Get if Any Horizontal Keys pressed ((A or D) or (Left or Right) Buttons)
    Vector2 movement = new Vector2 (moveHorizontal, 0f);	//Put moveHorizontal in a Vector2 Variable (x,y)...moveHorizontal will be the x axis
    rb2D.velocity = movement * paddleSpeed;		//Add Velocity to the player ship rigidbody by multiplying the movement with paddleSpeed

        //Lock the position of the Paddle in the screen by putting a boundaries
        rb2D.position = new Vector2 (Mathf.Clamp (rb2D.position.x, boundary.xMin, boundary.xMax),-4f);
  }
}

Generated Script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Ball_script : MonoBehaviour {
  public float ballInitialVelocity = 600F;
  public Text txtScore;
  public AudioClip sfxBrickHit;
  public AudioClip sfxPaddleBorderHit;
  private Rigidbody2D rb2D;
  private AudioSource audioSource;
  private bool ballInPlay;
  private int totalScore;

  void Start() {
    rb2D = base.GetComponent<Rigidbody2D>();
    audioSource = base.GetComponent<AudioSource>();
  }

  void Update() {
    if(((Input.GetButtonDown("Jump") && (ballInPlay == false)) && GameManager_script.startGame)) {
      base.transform.parent = null as Transform;
      ballInPlay = true;
      rb2D.isKinematic = false;
      if((Input.GetAxis("Horizontal") == 0F)) {
        rb2D.AddForce(new Vector2(1F, ballInitialVelocity));
      } else if((Input.GetAxis("Horizontal") > 0F)) {
        rb2D.AddForce(new Vector2(ballInitialVelocity, ballInitialVelocity));
      } else if((Input.GetAxis("Horizontal") < 0F)) {
        rb2D.AddForce(new Vector2(-(ballInitialVelocity), ballInitialVelocity));
      }
    }
  }

  void OnCollisionEnter2D(Collision2D coll) {
    if((coll.gameObject.tag == "Brick")) {
      audioSource.clip = sfxBrickHit;
      audioSource.Play();
      totalScore += coll.gameObject.GetComponent<Brick_script>().score;
      txtScore.text = totalScore.ToString("000000");
      Object.Destroy(coll.gameObject);
    }
    if(((coll.gameObject.tag == "Paddle") || (coll.gameObject.tag == "Border"))) {
      audioSource.clip = sfxPaddleBorderHit;
      audioSource.Play();
    }
  }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class GameManager_script : MonoBehaviour {
  public GameObject mainMenuPanel;
  public GameObject gameOverPanel;
  public static bool startGame = false;
  public static bool gameOver = false;

  void Update() {
    if((Input.GetButtonDown("Jump") && (startGame == false))) {
      startGame = true;
      mainMenuPanel.SetActive(false);
    }
    if((Input.GetButtonDown("Jump") && (gameOver == true))) {
      startGame = false;
      gameOver = false;
      SceneManager.LoadScene("MainGame");
    }
  }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameOverDetector_script : MonoBehaviour {
  public GameObject gameOverPanel;

  void OnCollisionEnter2D(Collision2D coll) {
    if((coll.gameObject.tag == "Ball")) {
      Object.Destroy(coll.gameObject);
      GameManager_script.gameOver = true;
      gameOverPanel.SetActive(true);
    }
  }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[System.SerializableAttribute]
public class Boundary : object {
  public float xMin = 0F;
  public float xMax = 0F;
}

public class Paddle_script : MonoBehaviour {
  public Boundary boundary = new Boundary();
  public float paddleSpeed = 0F;
  private Rigidbody2D rb2D;

  void Start() {
    rb2D = base.GetComponent<Rigidbody2D>();
  }

  void Update() {
    float moveHorizontal = 0F;
    Vector2 movement = Vector2.zero;
    moveHorizontal = Input.GetAxis("Horizontal");
    movement = new Vector2(moveHorizontal, 0F);
    rb2D.velocity = (movement * paddleSpeed);
    rb2D.position = new Vector2(Mathf.Clamp(rb2D.position.x, boundary.xMin, boundary.xMax), -4F);
  }
}

 

Graphs preview:

 

Requirement

Unity 3D Version 5.6 or higher.
uNode – Visual Scripting Version 1.5.1 or higher, this requirement is optional.

 

Download

Was this helpful to you?