When you start scripting with UnityScript, the amount of information out there can be quite intimidating. The library of functions is huge, but sometimes, all you want is to get started, even with a small/simple project to understand the basics of coding with Unity. So if you have been trying for a while to get started with coding and Unity with little or no programming background, then this tutorial is for you.
So, what are the 5 functions that can get you started quickly:
- Start: this function is called once (when the scene is loaded) and can be used to initialize variables that will be used throughout your script or the game.
- Update: this functions is called every time the screen is refreshed (i.e., every frame). It is a great place to, for example, update the time (using Time.deltaTime). It looks similar to the function FixedUpdate but differs in that FixedUpdate is to be used essentially for physics based animations.
- OnControllerColliderHit: This function is called when your controller collides with another object. It returns an object of type ControllerColliderHit from which you can get the other collider involved in the collision, as well as the corresponding object. If, for example, you have added a tag to most objects in the scene, this will, for example, make it possible to determine the tag of the object you have collided with, and perform actions accordingly. This function is to be added to a script that is a component of a character controller.
function OnControllerColliderHit(hit : ControllerColiderHit) { var tagOfOtherObject = hit.collider.gameObject.tag; print ("You have collided with " + tagOfOtherObject); }
- OnCollisionEnter: This function, can be used to detect collision between objects (as opposed to testing collision from a controller). For example, if we add this script to a box called box1, and another box box2 collides with it, we can detect the collision as well as the collider and label of the other object involved in the collision. This function is to be added to a script that is a component of an object.
- Destroy: this function is handy when you would like, for example, to destroy objects that you want to collide with. For example Destroy (gameObject) will destroy the game object gameObject.
- Application.LoadLevel: this function is used to load a new level. You can use it to, for example, load a Splashscreen, the instructions screen, and the different levels in the game. Nefore doing so, the level needs to be added to the build settings.
- Collect objects with the tag “pickme” upon collision
- Count number of objects collected
- If the number exceeds 5 then go to next level
- If the time is more than 60 seconds then load the level called “game over”
function Start() { var score:int; var thePlayer:GameObject = GameObject.FindWithTag("player"); }
function Update() { time = time + Time.deltaTime; }
function OnCollisionEnter(collision: Collision) { var tagOfOtherObject = collision.collider.gameObject.tag; print ("You have collided with " + tagOfOtherObject); }
function OnCollisionEnter(Collision: collision) { var otherObject:GameObject = collision.collider.gameObject; Destroy (otherObject); }
function Update() { if (score >=5) Application.LoadLevel ("nextLevel"); }
Putting it all Together
Now, we could create a very simple game using the following process:
You can create a new scene, add boxes with the tag “pickme”, add a FPS contoller, and add this script to the controller.
var time:float; var score:int; function Start () { score = 0; time = 0; } function Update() { time += Time.deltaTime; if (time >= 60) Application.LoadLevel("gameOver"); } function OnControllerColliderHit(hit:ControllerColliderHit) { if (hit.collider.gameObject.tag == "pickme") { print ("Just collided with " + hit.collider.gameObject.tag); Destroy (hit.collider.gameObject); print ("Score = "+score); score++; } if (score == 5) Application.LoadLevel("nextLevel"); }
Download the Project Here
Feeling stuck? no problem, you can download the full project here.
I would love to hear your feedback
Did you manage to create a simple game with this code, if so, what was it? Please leave a comment. Thanks!