Unity Scripts

NOTE: Allot of these scripts are far from perfect. These scripts helped me out, and I tried to adjust them to my needs, but I am not a brilliant programmer. Whille posting these scripts I noticed allot of things I would do differently now, I'll do my best to make improvements to these scripts in the near future.

Level Switcher Script
As seen on the Unity help pages, a script for switching between your levels. My script uses the number keys, but you can change this to anything you want. The script loads a level, like loadlevel.1 stands for the first level you have set in your build settings, loadlevel .2 for the second, and so on.

function Update(){

if (Input.GetKey(KeyCode.Alpha1)){
Application.LoadLevel(0);
}
else if (Input.GetKey(KeyCode.Alpha2)){
Application.LoadLevel(1);
}
else if (Input.GetKey(KeyCode.Alpha3)){
Application.LoadLevel(2);
}
else if (Input.GetKey(KeyCode.Alpha4)){
Application.LoadLevel(3);
}
else if (Input.GetKey(KeyCode.Alpha5)){
Application.LoadLevel(4);
}
else if (Input.GetKey(KeyCode.Alpha6)){
Application.LoadLevel(5);
}
else if (Input.GetKey(KeyCode.Alpha7)){
Application.LoadLevel(6);
}
else if (Input.GetKey(KeyCode.Alpha8)){
Application.LoadLevel(7);
}
else if (Input.GetKey(KeyCode.Alpha9)){
Application.LoadLevel(8);
}
}

Camera position switch script
My first scene consists of a number of tunnels, the camera goes through these tunnels, and by pressing a key the camera can switch between them. My biggest trouble at the time was that all the tunnels vary in length, and ofcourse when the camera reaches the end, it should start over again at the beginning. That's why I made a boolean (true or false statement) for every tunnel. Pressing a button (in case of the script, it's P,O,I or U) will set a statement true, say for instance Pyramid Tunnel = true. It will also set the camera X postion, and thereby placing it in the pyramid tunnel. When Pyramid Tunnel = true, it will set the maximum Z position for the camera to the end of the pyramid tunnel.
It also features keys for adjusting the speed of the camera, and for adjusting the field of view of the camera.

var tunnel = true;
var pyramid = false;
var sphere = false;
var poly = false; //-5909.885 //1750
var speedcam = 1;
var einde = false;

function Update () {

if(Input.GetKeyDown(KeyCode.Z)){
    speedcam += 1;
    }
if(Input.GetKeyDown(KeyCode.X)){
    speedcam -= 1;
    }
if(Input.GetKey(KeyCode.C)){
    speedcam = 1;
    }

transform.position.z += speedcam;

if (Input.GetKey(KeyCode.O)){
    tunnel = false;
    pyramid = true;
    sphere = false;
    poly = false;
    transform.position.x = 1145.834;
    }

if (Input.GetKey(KeyCode.P)){
    tunnel = true;
    pyramid = false;
    sphere = false;
    poly = false;
    transform.position.x = -799.4958;
    }
   
    if (Input.GetKey(KeyCode.I)){
    tunnel = false;
    pyramid = false;
    sphere = true;
    poly = false;
    transform.position.x = -2796.621;
    }
   
    if (Input.GetKey(KeyCode.U)){
    tunnel = false;
    pyramid = false;
    sphere = false;
    poly = true;
    transform.position.x = -5909.885;
    }

if(tunnel == true){

    if (transform.position.z > 1420) //2600)
    {

        transform.position.y = 14.53708;
        transform.position.x = -799.4958;
        transform.position.z = -711.9977;

    }
}

if(pyramid == true){
    if (transform.position.z > 2600) //2600)
    {

        transform.position.y = 14.53708;
        transform.position.x = 1145.834;
        transform.position.z = -711.9977;

    }
}

if(sphere == true){
    if (transform.position.z > 2600) //2600)
    {

        transform.position.y = 14.53708;
        transform.position.x = -2796.621;
        transform.position.z = -711.9977;

    }

}

if(poly == true){
    if (transform.position.z > 1750)
    {

        transform.position.y = 14.53708;
        transform.position.x = -5909.885;
        transform.position.z = -711.9977;

    }

}
if (Input.GetKey(KeyCode.M)){
    einde = true;
    }

if (einde == true){
    camera.fieldOfView += 0.2;
    }
if(camera.fieldOfView >= 179){
    einde = false;
    }
if (Input.GetKey(KeyCode.N)){
    camera.fieldOfView = 60;
    }


}

Camera Rotate Around Script
Really simple script, I think it comes directly out of a example project provided by unity. I really like the public variables (a varaible you can change in the unity editor) this helped me testing script on different objects without constantly adding scripts to them. So, to make this work, just drag an object out of the hierachy on the target variable in the Unity Inspector.

var degrees = 10;
var target : Transform;

function Update() {

    transform.RotateAround (target.position, Vector3.up, degrees * Time.deltaTime);

}

Create a strobe light
A script for turning your regular light, into a strobe. In the inspector you can tweek the speed and threshold of the strobe.


var minTime = .5;
var thresh = .5;

private var lastTime = 0;
private var myLight;

function Start()
{
    myLight = GetComponent(Light);
}

function Update ()
{

    if ((Time.time - lastTime) > minTime)
        if (Random.value > thresh)
            light.enabled = true;
        else
            light.enabled = false;
    lastTime = Time.time;
}


Camera Glitch Script
A script that makes the camera glitch out. If you checked out my screenshots, or the clips of my performance you've probably seen it. The effect is pretty simply actually. The script sends a random value to the Normalized View Port Rect. This makes te background go all glitchy. By adjusting the clear flags you can set it to the background or to the entire scene. With the N key you set the clearflags, with the B key, you send the random values.


function Update () {

if(Input.GetKeyDown(KeyCode.N) && camera.clearFlags == CameraClearFlags.Nothing){
    camera.clearFlags = CameraClearFlags.Depth;
} else if(Input.GetKeyDown(KeyCode.N) && camera.clearFlags == CameraClearFlags.Depth){
    camera.clearFlags = CameraClearFlags.Nothing;
    }

if (Input.GetKey(KeyCode.B)) {
        // choose the margin randomly
        var margin = Random.Range (0.0, 0.4);
        // setup the rectangle
        camera.rect = Rect (margin, 0, 1 - margin * 2, 1);
    }

if (Input.GetKeyUp(KeyCode.B)) {
        // setup the rectangle
        camera.rect = Rect (0, 0, 1 - 0 * 2, 1);
    }

if (Input.GetKey(KeyCode.W)){
    camera.fieldOfView -= 0.6;
    }
if (Input.GetKey(KeyCode.S)){
    camera.fieldOfView += 0.6;
    }

}


Mesh switching script
A sript that allows you to easily switch between meshes. Why meshes, and not game-objects? Easy, because of other already attached scripts! I used this on an object that moved around randomly, and by switching between the meshes, the position would reamain the same. Not only the position, but any other active scripts would still be active on the game-object after switching. So, I made some public variables, you can dragg your mess on them, and switch between them. Like so,

var meshToUse : Mesh;
var original : Mesh;

function Update () {

if(Input.GetKey(KeyCode.P)){
GetComponent(MeshFilter).mesh = meshToUse;
}

if(Input.GetKey(KeyCode.O)){
GetComponent(MeshFilter).mesh = original;
}

}

Enable/Disable multiple children
I had multiple objects in a scene, which I wanted do disable with one keystroke. I also wanted to be able to enable them with one keystroke. This script helped me do just that. You need an Empty, en put all the objects you like to enable / disable in it. Attach this script to the empty, and you can disable with Q and enable with E.

function Update () {
var renderers = GetComponentsInChildren(Renderer);

if(Input.GetKey(KeyCode.Q)){
for (var r : Renderer in renderers) {
    r.enabled = false;
} }

if(Input.GetKey(KeyCode.E)){
for (var r : Renderer in renderers) {
    r.enabled = true;
} }



Orthographic Camera Script
This script only works with the camera projection mode set to Orhographic. Instead of giving you a normal view, it let's you see through objects. Sounds boring, but it has allot to do with the objects used in the scene. Besides the control keys (W,A,S,D), there are the Z,X,C keys. Z allows you to zoom in, and X makes you zoom out. With C you send a random value to the zoom. I also made some presets under P,O,I.

function Update () {

if(Input.GetKey(KeyCode.W)){
    transform.position.z ++;
    }
if(Input.GetKey(KeyCode.S)){
    transform.position.z --;
    }

if(Input.GetKey(KeyCode.A)){
    transform.position.x ++;
    camera.clearFlags = CameraClearFlags.Depth;
    }

if(Input.GetKey(KeyCode.D)){
    transform.position.x --;
    camera.clearFlags = CameraClearFlags.Depth;
    }

if(Input.GetKey(KeyCode.Z)){
    camera.orthographicSize ++;
    camera.clearFlags = CameraClearFlags.Skybox;
    }

if(Input.GetKey(KeyCode.X)){
    camera.orthographicSize --;
    }

if(Input.GetKey(KeyCode.C)){
    var random = (Random.Range(7900, 8900));
    transform.position.z = random;
}

if(Input.GetKey(KeyCode.I)){
    camera.orthographicSize = 517;
    }

if(Input.GetKey(KeyCode.O)){
    camera.orthographicSize = 290;
    }

if(Input.GetKey(KeyCode.P)){
    camera.orthographicSize = 100;
    }

}

Random Position Camera Script
Script lets you set two values for the X and the Z axis, it will set a random range between those values.

var rangeZMin = 600;
var rangeZMax = 1900;
var rangeXMin = -300;
var rangeXMax = 900;

function Update () {

if(Input.GetKey(KeyCode.Z)){
    transform.position.z = Random.Range(rangeZMin,rangeZMax);
    transform.position.x = Random.Range(rangeXMin,rangeXMax);
    }

}