Guide:Unity: Difference between revisions

From Game Making Tools Wiki
(Created - some structural things, and copied over some examples I posted previously on Glorious Trainwrecks)
 
(Made 'instantiate' more ordinaryily descriptive, added beginnings on problem solving/debugging section)
Line 40: Line 40:
</pre>
</pre>
Like '''reset.js''' just attach this to any object and it should work. This has the <code>Escape</code> key set to quit the game.
Like '''reset.js''' just attach this to any object and it should work. This has the <code>Escape</code> key set to quit the game.
====Instatiate====
<code>
</code>


====Raycasting====
====Raycasting====
Line 61: Line 57:


You can also lock the cursor in place using <code>Screen.lockCursor</code>.
You can also lock the cursor in place using <code>Screen.lockCursor</code>.
====Moving things====


====Creating and destroying objects====
====Creating and destroying objects====
=====Instantiate (summoning objects)=====
<code>
</code>
=====keepPosOnReset.js=====
=====keepPosOnReset.js=====
<pre>
<pre>
Line 70: Line 73:
</pre>
</pre>
Any object you attach this script to will not have its position reset when that '''[[#reset.js|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.
Any object you attach this script to will not have its position reset when that '''[[#reset.js|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.
====Debugging / identifying problems / aaaaaarg====
=====Using the console=====
<pre>
Debug.Log("This gam is v good");
</pre>
<pre>
Debug.Log(transform.position.y); //would tell you where the object the script is attached to is on the Y axis
</pre>
<pre>
var player : Transform;
function Update(){
    Debug.Log("Player's Y position is " player.position.y); //where 'player' is a variable that is editable in the inspector
    }
</pre>
Using the command 'Debug.Log' will write text to the console. This will appear when testing your game within Unity in the little status bar down the bottom of the screen, and in the console window (Window>Console / Ctrl+Shift+C). It's most useful when you use it to write text at different stages within your scripts to check if that step is reached, and to write variables to check what they're up to. Very useful when you get stuck scripting!
=====Displaying in-game=====
Apart from / as well as utilising the console, you can also use Unity's UI stuff to display similar information which will also appear when you do a proper build of your game.

Revision as of 15:55, 29 December 2015

Interface

...

Scripting

Scripting in Unity is done in either Javascript or C#. You can mix and match them in your project. Don't worry about trying to learn either comprehensively. The main thing is knowing what to search for, and trying to understand the scripts you find online (that you will then nick, and stick in your game). Try not to worry about doing things a 'right' way, do things in the way that makes sense to you (as long as it works!)

Scripts can be a bunch of separate files, and you can attach multiple to one object with no problems.

Generally I find it useful to create an empty game object that I can atach 'general' kinda scripts too ; for things like reseting the games, for example. But you can also attach these to your player, or whereever makes sense to you.

Example scripts

Working with buttons

Resetting, quitting, changing scenes

Attach these to anything.

reset.js
function Update () {
	if (Input.GetKeyDown(KeyCode.Backspace)) {
		Application.LoadLevel (1);
	}
}

You can replace KeyCode.Backspace with the name of whichever key 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).

You can replace the 1 in Application.LoadLevel (1) with the number of whichver level you want to load. You can find the number by going to ???. Make sure to include all your scenes at the this point!

quit.js
function Update () {
	if (Input.GetKeyDown(KeyCode.Escape)) {
		Application.Quit ();
	}
}

Like reset.js just attach this to any object and it should work. This has the Escape key set to quit the game.

Raycasting

The Cursor

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.

Moving things

Creating and destroying objects

Instantiate (summoning objects)

keepPosOnReset.js
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.

Debugging / identifying problems / aaaaaarg

Using the console
Debug.Log("This gam is v good");
Debug.Log(transform.position.y); //would tell you where the object the script is attached to is on the Y axis
var player : Transform;

function Update(){
    Debug.Log("Player's Y position is " player.position.y); //where 'player' is a variable that is editable in the inspector
    }

Using the command 'Debug.Log' will write text to the console. This will appear when testing your game within Unity in the little status bar down the bottom of the screen, and in the console window (Window>Console / Ctrl+Shift+C). It's most useful when you use it to write text at different stages within your scripts to check if that step is reached, and to write variables to check what they're up to. Very useful when you get stuck scripting!

Displaying in-game

Apart from / as well as utilising the console, you can also use Unity's UI stuff to display similar information which will also appear when you do a proper build of your game.