Functions and asteroids

22/11/2016

This afternoon I learned about functions in coding. A function is a named re-usable variable for one specific task, e.g. void Update or void Store. Functions are called, which basically means that they are just used. An easy example of a function is Debug.log. Other examples of functions are:

  • void OnCollisionEnter (Collision collision)
  • int AddNumbers (int a, int b)

A function definition has 3 parts:

  • A return type e.g. string or void
  • A name/identifier e.g. Update or Start
  • A set of parameters (variables) – This is optional

A function call has two parts:

  • The function’s name
  • A set of parameters

Some examples of function calls include Destroy (gameObject); and AddNumbers (2, 3);

_______________________________________________________

After learning about functions, I then worked on my asteroids game. My main focus this lesson was to add a menu screen that would navigate between levels. I made a new scene, then went into Game Object, UI, and selected ‘button’. I then made a C# script and made some code that takes you to the first level. I then duplicated the button and made a really simple credits screen.

I also added collision to the game. I added a polygon collider to the spaceship (because a polygon collider can be made to fit the ship’s shape better) and added circle colliders to the asteroids. I then added a similar code to the scene manager code used on the menu, that makes it so that when you collide with an asteroid, the level reloads.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s