Skip to content

Instantly share code, notes, and snippets.

@victor-vargas2009
Created February 1, 2016 03:06
Show Gist options
  • Save victor-vargas2009/3a6838a181c8b1a2f6e4 to your computer and use it in GitHub Desktop.
Save victor-vargas2009/3a6838a181c8b1a2f6e4 to your computer and use it in GitHub Desktop.
Bloke Breaker
using UnityEngine;
using System.Collections;
public class Ball : MonoBehaviour {
private Paddle paddleObject;
private Vector3 paddleToBallVector;
private bool hasStarted = false;
// Use this for initialization
void Start () {
paddleObject = GameObject.FindObjectOfType<Paddle>();
paddleToBallVector = this.transform.position - paddleObject.transform.position;
}
// Update is called once per frame
void Update () {
if(!hasStarted){
this.transform.position = paddleObject.transform.position + paddleToBallVector;
if(Input.GetMouseButtonDown(0)){
hasStarted = true;
this.GetComponent<Rigidbody2D>().velocity = new Vector2(2.0f, 10.0f);
}
}
}
void OnCollisionEnter2D(Collision2D collision){
this.GetComponent<Rigidbody2D>().velocity += new Vector2(Random.Range(-0.1f,0.1f), Random.Range(-0.1f,0.1f));
if(hasStarted){
//this.GetComponent<AudioSource>().Play();
}
}
public void resetGame(){
hasStarted = false;
this.transform.position = paddleObject.transform.position + paddleToBallVector;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment