Skip to content

Instantly share code, notes, and snippets.

@CFranc111
Last active June 25, 2017 04:48
Show Gist options
  • Save CFranc111/3435aa52e7cee097a7e2 to your computer and use it in GitHub Desktop.
Save CFranc111/3435aa52e7cee097a7e2 to your computer and use it in GitHub Desktop.
Unity Block Breaker ball script as of 01.31.16
using UnityEngine;
using System.Collections;
public class Ball : MonoBehaviour {
public bool debug = false;
private Paddle paddle;
private bool hasStarted = false;
private string[] makeHitSounds = new string[6] {"Paddle", "LeftWall", "RightWall", "TopWall", "BottomWall", "Invincible"};
private Vector3 paddleToBallVector; // Paddle's position minus ball's position
private float nudgeFactor = 0.2f; // Max random nudge added to the velocity of each collision (to reduce boring play loops and increase ball speed)
private float maxSpeed = 15f;// Magnitude to constrain ball speed
//
// Initialization
//
void Start () {
paddle = GameObject.FindObjectOfType<Paddle>(); // Get this paddle
paddleToBallVector = this.transform.position - paddle.transform.position; // Used to initialize position below
}
//
// Called once per frame
//
void Update () {
// Reset position if game not yet started
if (!hasStarted) {
// Lock the ball relative to the paddle
this.transform.position = paddle.transform.position + paddleToBallVector;
// Wait for mouse click to launch
if (Input.GetMouseButtonDown(0)) {
hasStarted = true; // Won't run again once one mouse click occurs
// Set initial ball velocity
this.rigidbody2D.velocity = new Vector2 (2f, 10f); // x, y
}
}
}
//
// Generate a random float between zero and the nudgefactor
//
float RandomFloat() {
return Random.Range(0, nudgeFactor);
}
//
// Get the ball's velocity
//
Vector2 BallVelocity() {
Rigidbody2D rb = GetComponent<Rigidbody2D>();
return rb.velocity;
}
//
// Nudge the velocity to avoid boring play loops and speed up ball
//
void VelociNudge(Vector2 exitVelocity) {
// Build and apply nudge
Vector2 nudge;
switch (exitVelocity.x > 0) {
case false: // x is -
switch (exitVelocity.y > 0) {
case false: // y is -
nudge.x = -RandomFloat();
nudge.y = -RandomFloat();
this.rigidbody2D.velocity += nudge;
if (debug) print ("nudge: " + nudge);
break;
case true: // y is +
nudge.x = -RandomFloat();
nudge.y = RandomFloat();
this.rigidbody2D.velocity += nudge;
if (debug) print ("nudge: " + nudge);
break;
default:
Debug.LogError("Exit velocity y: " + exitVelocity.y);
break;
}
break;
case true: // x is +
switch (exitVelocity.y > 0) {
case false: // y is -
nudge.x = RandomFloat();
nudge.y = -RandomFloat();
this.rigidbody2D.velocity += nudge;
if (debug) print ("nudge: " + nudge);
break;
case true: // y is +
nudge.x = RandomFloat();
nudge.y = RandomFloat();
this.rigidbody2D.velocity += nudge;
if (debug) print ("nudge: " + nudge);
break;
default:
Debug.LogError("Exit velocity y: " + exitVelocity.y);
break;
}
break;
default:
Debug.LogError("Exit velocity x: " + exitVelocity.x);
break;
}
}
//
// When ball exits a collision
//
void OnCollisionExit2D(Collision2D coll) {
if (hasStarted) { // Don't apply at very beginning when ball placed on paddle
if (System.Array.IndexOf(makeHitSounds, coll.gameObject.name) > -1) {
audio.Play(); // Play basic hit sound if appropriate
}
// Get the ball's outbound velocity
Vector2 exitVelocity = BallVelocity();
// Reset magnitude if ball gets too fast
if (debug) print("Magnitude: " + this.rigidbody2D.velocity.magnitude);
if (exitVelocity.magnitude > maxSpeed) {
this.rigidbody2D.velocity = exitVelocity = this.rigidbody2D.velocity.normalized * maxSpeed; // normalize = make magnitude 1
}
// Apply velocity nudge
VelociNudge(exitVelocity);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment