Skip to content

Instantly share code, notes, and snippets.

View OutlawGameTools's full-sized avatar

J. A. Whye OutlawGameTools

View GitHub Profile
@OutlawGameTools
OutlawGameTools / pickups.js
Created February 11, 2015 22:13
Unity: Attach to game objects that can be picked up (collider2d, isTrigger). Play audio if it exists on game object.
#pragma strict
public var pickupValue : int = 5;
function OnTriggerEnter2D(other : Collider2D)
{
var gObj : GameObject = other.gameObject;
var clip : AudioClip;
if (gObj.CompareTag("Player"))
@OutlawGameTools
OutlawGameTools / DestroyOnClick.js
Last active August 29, 2015 14:16
CS109 - Destroy the game object when clicked on. Game object must have a collider component.
#pragma strict
function OnMouseDown ()
{
if (gameObject.transform.position.y > -2 && gameObject.transform.position.y < 4)
{
GameData.numGrabbed++;
GameData.DisplayScore();
Destroy(gameObject);
}
@OutlawGameTools
OutlawGameTools / BombHit.js
Created March 2, 2015 05:37
CS109 - Destroy the game object when it collides with a specific object (based on tag).
#pragma strict
public var hitTag : string = "Ground";
function OnCollisionEnter2D (other : Collision2D)
{
if (other.gameObject.CompareTag(hitTag))
{
GameData.numMissed++;
GameData.DisplayScore();
@OutlawGameTools
OutlawGameTools / InstantiateGObj.js
Created March 2, 2015 05:50
CS109 - Instantiate (clone) a game object in the scene. Also shows InvokeRepeating() to clone multiples
#pragma strict
public var gObj : GameObject;
function Start () {
InvokeRepeating("CreateRandom", 0, 4);
}
function CreateRandom ()
{
@OutlawGameTools
OutlawGameTools / GameData.js
Created March 2, 2015 06:13
CS109 - A place to store our score variables.
#pragma strict
// access from other scripts like GameData.numGrabbed++
static var numGrabbed : int = 0;
static var numMissed : int = 0;
static function DisplayScore () {
Debug.Log("Grabbed: " + numGrabbed + " Missed: " + numMissed);
@OutlawGameTools
OutlawGameTools / Accelerate.js
Created March 9, 2015 00:19
CS109 - Add force to a RigidBody2D when the mouse button is pressed.
#pragma strict
public var rocket : GameObject; // object to push
public var xSpeed : float = 0; // horizontal speed
public var ySpeed : float = 400; // vertical speed
private var rb2D : Rigidbody2D;
function Start ()
{
@OutlawGameTools
OutlawGameTools / Explosion.js
Created March 9, 2015 00:33
CS109 - Attach to an explosion, it deletes it after done. Optionally kills Enemies caught inside blast radius.
#pragma strict
public var liveTime : float = 0.5f; // how long to wait before ditching the explosion
public var killWithTag : String = "Enemy"; // kills these tagged objects in the blast radius
function Start ()
{
Invoke("DeleteMe", liveTime);
}
@OutlawGameTools
OutlawGameTools / Particle.js
Last active August 29, 2015 14:16
CS109 - Generic utilities for particles, such as bring particle up to a named layer so it can be seen.
#pragma strict
public var layerName : String = "Foreground"; // the layer in which the particle should be placed
public var orderInLayer : int = -1; // -1 to go behind the parent object, 1 to go in front of it
public var destroyAfterTime : float = 0;
private var pSys : ParticleSystem;
function Start () {
@OutlawGameTools
OutlawGameTools / MoveObject.js (Phase 1)
Created March 9, 2015 09:21
CS109 - Move a game object using Rigidbody2D.AddForce. (Phase 1 of this script)
#pragma strict
public var speed : int;
function Start ()
{
var newSpeed : Vector2;
newSpeed.x = speed;
newSpeed.y = 0;
@OutlawGameTools
OutlawGameTools / MoveObject.js (Phase 2)
Created March 11, 2015 08:49
CS109 - A better version of MoveObject that adds randomness and is more generic.
#pragma strict
public var xSpeed : int;
public var ySpeed : int;
public var randomX : boolean = false;
public var minXSpeed : int = 100;
public var maxXSpeed : int = 200;
public var randomY : boolean = false;