Guide:Unity: Difference between revisions

From Game Making Tools Wiki
(→‎Getting Started: Tutorial links)
m (spell checks, fixed a link)
 
(12 intermediate revisions by one other user not shown)
Line 1: Line 1:
Exmaple scripts, tricks, and other things to help you use [[Unity 3D]]
Exmaple scripts, tricks, and other things to help you use [[Unity]].


==Getting Started==
==Getting Started==
Line 6: Line 6:
===Tutorials===
===Tutorials===
* [http://workshops.ko-opmode.com/ How to make cool stuff in Unity] - A lot of people seemed fond of this one. It completely avoids coding, unlike Unity's tutorials.
* [http://workshops.ko-opmode.com/ How to make cool stuff in Unity] - A lot of people seemed fond of this one. It completely avoids coding, unlike Unity's tutorials.
* [https://unity3d.com/learn/tutorials/projects/roll-ball-tutorial Roll-A-Ball Tutorial] - I tihnk this is the best of Unity's own tutorials, but I'd do the above first.
* [https://learn.unity.com/project/roll-a-ball Roll-A-Ball Tutorial] - I think this is the best of Unity's own tutorials, but I'd do the above first.
* [https://www.youtube.com/watch?time_continue=36&v=HZrNzXsjji8 How to make a simple Walking Simulator style game] - Good intro to quickly getting together a playable thing!


==Interface==
==Interface==
Line 13: Line 14:
===Lighting===
===Lighting===
====Shadows====
====Shadows====
Sometiems you may notice that shadows far from the camera are not drawn. A resource intensive way to get round this is to go to Edit>Project Settings>Quality, and change the 'Shadow Distance' property.
Sometimes you may notice that shadows far from the camera are not drawn. A resource-intensive way to get around this is to go to Edit>Project Settings>Quality, and change the 'Shadow Distance' property.


==Exporting==
==Exporting==
Though exporting from Unity is a pretty straightforward process, there are a lot of optional things, and some [[:Category:Platforms|platform]]-specific things to take into account.
Though exporting from Unity is a pretty straightforward process, there are a lot of optional things, and some [[:Category:Platforms|platform]]-specific things to take into account.


===File Size===
Unity isn't perfect at reducing the size of exported games, but this video has some tips: [https://hooktube.com/7O21c8BzEzM Reduce your Unity Build Size with some quick shrinking Tips (mobile, windows, etc)]
The big one I learned from that is checking the ''editor log'' to get details about what is taking up space.
===Options===
====Borderless Fullscreen====
====Icons====
You can add custom icons to your games and enter your name by  
You can add custom icons to your games and enter your name by  


Borderless Fullscreen
===Platform Notes===
====Linux====
For exporting to Linux you should make sure you use the ''Universal'' option. Unlike Windows Linux does not automatically run 32-bit executable files without installing extra stuff. If someone's into games they have probably already done this, but it's good to factor in all circumstances. If you have a README you may want to mention that a Linux user will have to make the game executable by using <code>chmod +x</code> and then, when running it include a <code>./</code>before the file name.


===Linux===
For exporting to Linux you should make sure you use the Universal option. Unlike Windows Linux does not automatically run 32bit executable files without instaling extra stuff. If someone's into games they have probably already done this, but it's good to factor in all circumstances. If you have a README you may want to mention that a Linux user will have to make the game executable by using <pre>chmod +x</pre> and then, when running it include a <pre>./</pre>before the file name.
So their terminal should look something like this:
So their terminal should look something like this:
<pre>
<pre>
Line 29: Line 38:
$ ./examplegame.x86_64
$ ./examplegame.x86_64
</pre>
</pre>
[[Distribution:Itch.io|Itch.io]] is great because it will take care of this for players, as well as keep things nice and organised and Linuxy!
[[Distribution:Itch.io|Itch.io]] is great because it will take care of this for players, as well as keep things nice and organised and Linuxy!
If you are hiding the mouse cursor you will also need to lock it's location, otherwise, if for example you've made a first-person game with mouse = WASD controls, they will have restrictions on rotating thier view.
If you are hiding the mouse cursor you will also need to lock it's location, otherwise, if for example you've made a first-person game with mouse = WASD controls, they will have restrictions on rotating thier view.
===Mac OS===
 
Pretty similar requirements as as Linux, in that you should lock the cursor, and use the 'Univesal' build type.
====Mac OS====
===Windows===
Pretty similar requirements as as Linux, in that you should lock the cursor, and use the ''Universal'' build type.
Exporting for Windows is pretty easy. 32bit builds will work on anything, but you can also include
 
====Windows====
Exporting for Windows is pretty easy. 32bit builds will work on anything, but you can also include 64bit ones together or as a separate download for people that want them.
 
Windows builds end up with two .PDB files in their directory which are just debugging stuff and you can delete them to make your game a much smaller download (they can make it at least twice as big D: )


==Importing==
==Importing==
===3-D Models===
===3-D Models===
====Blender====
====Blender====
In [[Blender]] the forward direction is X and up is Z, but in Unity forward is Z and Y is up! Which means you'l get an irritating surprise when importing things! There are some solutions though:
In [[Blender]] the forward direction is X and up is Z, but in Unity forward is Z and Y is up! Which means you'll get an irritating surprise when importing things! There are some solutions though:


(just dumping some links, will sort later)
(just dumping some links, will sort later)
Line 57: Line 72:
Scripts can be a bunch of separate files, and you can attach multiple to one object with no problems.
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 attach 'general' kinda scripts to ; for things like reseting the games, for example. But you can also attach these to your player, or whereever makes sense to you.
Generally I find it useful to create an empty game object that I can attach 'general' kinda scripts to ; for things like resetting the games, for example. But you can also attach these to your player, or whereever makes sense to you.


===Example scripts===
===Example scripts===


====Audio====
* [[Guide:Unity_3D_scenes|Resetting, quitting, changing scenes]]
* [[Guide:Unity_3D_debugging|Debugging, Problem solving]]
* [[Guide:Unity_3D_GUI|Working with text and the GUI]]


====Working with buttons====
====Colliders====
=====Enabling and disabling=====
Most of the posts I found via searching say you can't just switch colliders off and on with scripting, but I guess those're all outa date because it works just fine for me!


====Resetting, quitting, changing scenes====
<pre>GameObject.GetComponent(Collider).enabled = false;</pre>


Attach these to anything.
Either remove <code>GameObject.</code> if the script's on the object with the collider you want to toggle, or use a variable (or search, etc..) to put the name of the GameObject you want to reference.


=====reset.js=====
======Other Methods======
<pre>
May as well mention the work'rounds I dug up though! They include: Deactivating the collider by enabling it as a trigger. This'll cause an 'OnTriggerEnter' state to be called rather than 'OnCollisionEnter'. Hacky, but it works!, as long as you're using a convex shaped collider
function Update () {
if (Input.GetKeyDown(KeyCode.Backspace)) {
Application.LoadLevel (1);
}
}
</pre>
You can replace <code>KeyCode.Backspace</code> with [http://docs.unity3d.com/ScriptReference/KeyCode.html the name of whichever key] you wish. You could also replace it with, for example <code>"fire1"</code> or <code>"jump"</code>, 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 <code>1</code> in <code>Application.LoadLevel (1)</code> 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=====
<pre>
function Update () {
if (Input.GetKeyDown(KeyCode.Escape)) {
Application.Quit ();
}
}
</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.


====Colliders====
<pre>GetComponent(Collider).isTrigger = true;</pre>
=====Enabling and disabling=====
Most of the posts I found via searching say you can't just switch colliders off and on with scripting, but I guess those're all outa date because it works just fune for me!
<pre>
GameObject.GetComponent(Collider).enabled = false;
</pre>
Either remove'GameObject.' if the script's on the object with the colider you want to toggle, or use a variable (or search, etc..) to put the name of the GameObject you want to reference.


======Other Methods======
May as well mention the work'rounds I dug up though! They include: Deativating the collider by enabling it as a trigger. This'll cause an 'OnTriggerEnter' state to be called rather than 'OnCollisionEnter'. Hacky, but it works!, as long as you're using a convex shaped collider
<pre>
GetComponent(Collider).isTrigger = true;
</pre>
Also hacky!, but you can shrink the collider down to 0 to make it impossible to actually be triggered by anything D: then resize it when needed D:
Also hacky!, but you can shrink the collider down to 0 to make it impossible to actually be triggered by anything D: then resize it when needed D:
And of course, if you're able to, you could disable the whole game obejct. Another way to use this could be to have two copies of the same object which you switch out/in.
And of course, if you're able to, you could disable the whole game object. Another way to use this could be to have two copies of the same object which you switch out/in.


====Raycasting====
====Raycasting====
<code>
<code>
</code>
</code>
====The Cursor====
=====hideCursor.js=====
<pre>
function Start () {
Screen.showCursor = false;
}
</pre>
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 <code>Screen.showCursor</code> to <code>true</code> when your menu is called.
You can also lock the cursor in place using <code>Screen.lockCursor</code>.


====Moving things====
====Moving things====
Line 155: Line 131:
</pre>
</pre>


====Debugging / identifying problems / aaaaaarg====
===See Also===
=====Using the console=====
* [http://wiki.unity3d.com/index.php/Scripts Scripts] section on the Unify Community Wiki.
<pre>
* [https://stopsecretdesign.wordpress.com/2011/09/19/unity-spaceship-tutorial/ Unity Spaceship Tutorial].
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.
 
To show the contents of a variable on screen you can use:
<pre>
function OnGUI() {
// Show timer
GUI.Label(Rect(0,0,100,100), Time.time.ToString());
}
</pre>
In this case it displays a timer, which starts when the game does. Time.time is handled by Unity automatically, and can't be modified.
 
You can use any other variable, just make sure you append .ToString() — this converts the value to a text string, which is the only way it can be displyed with this method.
 
You can mess with the values in the brackets after 'Rect', as they currently are they'll show the value in the top left of the screen.


==Other Resources==
==Other Resources==
* [[Guide:Sources#Unity]] - Example projects to poke round in, in the sources section of this wiki.
* [http://wiki.unity3d.com/index.php/Main_Page Unify Community Wiki] - Can be outdated, but has lots of scripts and things up there.
* [http://wiki.unity3d.com/index.php/Main_Page Unify Community Wiki] - Can be outdated, but has lots of scripts and things up there.
** [http://wiki.unity3d.com/index.php/Tips Tips] page on the Unify Community Wiki.
* [https://www.youtube.com/watch?v=OOeA0pJ8Y2s Unite 2015 - A Little Math for Your Big Ideas] - Floating, a related watery physics.
* [https://www.youtube.com/watch?v=OOeA0pJ8Y2s Unite 2015 - A Little Math for Your Big Ideas] - Floating, a related watery physics.
* [http://catlikecoding.com/unity/tutorials/ Unity C# Tutorials] on Catlike Coding. Lots of neat, procedural stuff here.
* [http://catlikecoding.com/unity/tutorials/ Unity C# Tutorials] on Catlike Coding. Lots of neat, procedural stuff here.
* [http://forum.unity3d.com/threads/newbie-guide-to-unity-javascript-long.34015/ Newbie Guide to Unity JavaScript] - I think this should be helpful to people who've never programmed before?
* [http://forum.unity3d.com/threads/newbie-guide-to-unity-javascript-long.34015/ Newbie Guide to Unity JavaScript] - I think this should be helpful to people who've never programmed before?
* [[Assets:Main_Page#HLSL|HLSL]] shaders in the assets section.
* [https://uninomicon.com/ uninomicon.com] - Wiki documenting the undocumented quirks of the Unity engine.

Latest revision as of 17:42, 6 January 2023

Exmaple scripts, tricks, and other things to help you use Unity.

Getting Started

(resources for starting using unity)

Tutorials

Interface

...

Lighting

Shadows

Sometimes you may notice that shadows far from the camera are not drawn. A resource-intensive way to get around this is to go to Edit>Project Settings>Quality, and change the 'Shadow Distance' property.

Exporting

Though exporting from Unity is a pretty straightforward process, there are a lot of optional things, and some platform-specific things to take into account.

File Size

Unity isn't perfect at reducing the size of exported games, but this video has some tips: Reduce your Unity Build Size with some quick shrinking Tips (mobile, windows, etc)

The big one I learned from that is checking the editor log to get details about what is taking up space.

Options

Borderless Fullscreen

Icons

You can add custom icons to your games and enter your name by

Platform Notes

Linux

For exporting to Linux you should make sure you use the Universal option. Unlike Windows Linux does not automatically run 32-bit executable files without installing extra stuff. If someone's into games they have probably already done this, but it's good to factor in all circumstances. If you have a README you may want to mention that a Linux user will have to make the game executable by using chmod +x and then, when running it include a ./before the file name.

So their terminal should look something like this:

$ chmod +x examplegame.x86_64
$ ./examplegame.x86_64

Itch.io is great because it will take care of this for players, as well as keep things nice and organised and Linuxy!

If you are hiding the mouse cursor you will also need to lock it's location, otherwise, if for example you've made a first-person game with mouse = WASD controls, they will have restrictions on rotating thier view.

Mac OS

Pretty similar requirements as as Linux, in that you should lock the cursor, and use the Universal build type.

Windows

Exporting for Windows is pretty easy. 32bit builds will work on anything, but you can also include 64bit ones together or as a separate download for people that want them.

Windows builds end up with two .PDB files in their directory which are just debugging stuff and you can delete them to make your game a much smaller download (they can make it at least twice as big D: )

Importing

3-D Models

Blender

In Blender the forward direction is X and up is Z, but in Unity forward is Z and Y is up! Which means you'll get an irritating surprise when importing things! There are some solutions though:

(just dumping some links, will sort later)

1. When Exporting
2. When Importing
3. In Unity

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 attach 'general' kinda scripts to ; for things like resetting the games, for example. But you can also attach these to your player, or whereever makes sense to you.

Example scripts

Colliders

Enabling and disabling

Most of the posts I found via searching say you can't just switch colliders off and on with scripting, but I guess those're all outa date because it works just fine for me!

GameObject.GetComponent(Collider).enabled = false;

Either remove GameObject. if the script's on the object with the collider you want to toggle, or use a variable (or search, etc..) to put the name of the GameObject you want to reference.

Other Methods

May as well mention the work'rounds I dug up though! They include: Deactivating the collider by enabling it as a trigger. This'll cause an 'OnTriggerEnter' state to be called rather than 'OnCollisionEnter'. Hacky, but it works!, as long as you're using a convex shaped collider

GetComponent(Collider).isTrigger = true;

Also hacky!, but you can shrink the collider down to 0 to make it impossible to actually be triggered by anything D: then resize it when needed D: And of course, if you're able to, you could disable the whole game object. Another way to use this could be to have two copies of the same object which you switch out/in.

Raycasting

Moving things

BallRolling.js
var force : float = 6;

function Update () {
	var h : float = Input.GetAxis("Horizontal");
	var v : float = Input.GetAxis("Vertical");
	GetComponent.<Rigidbody>().AddForce(Vector3(h, 0.0, v) * force);
} 

If you attach this to a ball, or something else roundish, you will be able to roll it round using WASD / arrow keys.

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.

Camera

follow.js

See Also

Other Resources