Unity 3D: scripts and other tips.

Blueberry Soft's picture

I've been having lots of fun learning how to do things in Unity, and wanted to have somewhere to share that information. My Unity skills are pretty rudimentary, but thought it might still be helpful to people. I figure I'll mostly be posting useful little scripts, and encourage you to do the same. Also feel free to ask questions!

So far I haven't used much scripting, and everything's been found through Google/Unity forum/Stack Overflow (maybe), but I do occasionally make a little tweak. I'm not a programmer, but I guess I understand general programming methodologies.

To use scripts you have to attach them to a game object. I've just learned the blank game objects are pretty handy for organising things (you can use them like folders), and you may also find them hand for storing general scripts.

Blueberry Soft's picture

Script: reset.js

function Update () {
	if (Input.GetKeyDown(KeyCode.Escape)) {
		Application.LoadLevel (0);
	}
}

Attach this to your blank game object or the player. This will reset the game to the first level.

You can replace 'KeyCode.Escape' with whatever you wish. You could also replace it with, for example "fire1" or "jump", whose keys are determined at edit>project settings>input, and may also be easily reconfigured by players (if given the option).

Blueberry Soft's picture

Script: keepPosOnReset.js (using DontDestroyOnLoad)

function Awake () {
	DontDestroyOnLoad (transform.gameObject);
}

Any object you attach this script to will not have its position reset when that reset.js script is called. I used this in something yesterday where apart from walking about and pushing things (placed by me) the player is able to create blocks. I wanted everything in the scene to reset normally on pressing 'Esc' except for the objects the player themself created. So some things are persistent until the game is quit.

Blueberry Soft's picture

Script: makeObject.js (using Instantiate)

var whichObject : GameObject;

function Update() {
	if (Input.GetButtonUp("Fire1"))
	Instantiate (whichObject, transform.position, transform.rotation);
}

This script creates an object (determined in the Unity editor, that's what the 'var whichObject : GameObject;' up the top does: it adds an option in the property inspector) at the player's location. The object is what is called a 'prefab': a game object which already has its material and physics all worked out and attached. You make a prefab like you would any other object in your scene, but you will need to copy it into your project's 'Assets' folder (and into any folder therein, defending on how you organise things). Once it exists in that assets folder you may delete it from the scene. I don't feel like I explained that well. Like in the reset.js script you can replace ' "fire1" ' with whichever button you like. The default for 'fire1' is 'Ctrl' in Windows.

Here's an example of a game with all the scripts to far: http://dl.dropboxusercontent.com/u/182517150/Play%20Space.html
'Ctrl' makes object, 'Space' jumps, 'Esc' resets, and 'W' 'A' 'S' 'D' and the mouse moves.

Blueberry Soft's picture

Making the object appear in front of the player.

var whichObject : GameObject;

function Update() {
	if (Input.GetButtonUp("Fire1"))
	Instantiate (whichObject, transform.position + transform.forward, transform.rotation);
}

This small addition makes the object appear in front of the player.

Blueberry Soft's picture

Script: hideCursor.js

function Start () {
	Screen.showCursor = false;
}

This little script will prevent the mouse cursor from being shown. Note that when previewing the game in Unity the cursor will still appear.

If you wish to control when it it shown or not, for example when a menu appears, simple set 'Screen.showCursor' to 'true' when your menu is called.

You can also lock the cursor in place using 'Screen.lockCursor'.

Blueberry Soft's picture

Script: quit.js

function Update () {
	if (Input.GetKeyDown(KeyCode.Delete)) {
		Application.Quit ();
	}
}

Like reset.js just attach this to any object and it should work. This has the 'delete' key set to quit the game (which is probably a bit odd, I admit).