To easily work out your game, it is good to align your game code through Pseudocode. This is artificial and informal language for developing algorithm.
Our game:
Drop the box
collect the box
Pseudocode
initialize boxcollected to zero
initialize totalboxes to zero
drop box
if collected
increase boxcollected by one
print boxcollected
end if
increase totalboxes by one
print totalboxes
The above forms bases of our code. This will be broken down into various functions in C#. It is self explanatory for a basic games. As we advance our story line, we will now be advancing our pseudo code. Every time you want to update your game, first update story line, then pseudo code before actual game coding.
It is now time to turn our pseudocode into C# code and display our score
Pseudocode
initialize boxcollected to zero
initialize totalboxes to zero
drop box
if collected
increase boxcollected by one
print boxcollected
end if
increase totalboxes by one
print totalboxes
In unity editor, inside assets folder, create a folder (from project menu, create -> folder) and name it Scripts. inside this folder, add C# script from project menu, create ->C# Script and name it CatchBox.
Double click our new script to open. It opens with the default code.
We begin by declaring variable to store boxes collected:
int boxescollected =0;
Then declare variable to store total boxes:
int totalboxes =0;
Every time a box is added to game, we add totalboxes: This will be added inside update method
totalboxes +=1;
We will use colliders added to the boxes to determine when the boxes collide. This denotes the box was collected. Every time the two boxes collide, we add boxescollected. We add a new method to detect collision and add
boxescollected +=1;
To know which box collided, we need to add tag to differentiate our boxes. Lets add tags "catch" and "box" and assign to our boxes.
Select box and in the inspector under tag, click add tags. In the tags, click + sign and add "catch" in tag 0. click + again and add "box" in tag 1.
Select our box again and set tag to "box". Select catch and set tag to "catch".
Save project.
Select "catch up" in the heirachy and drag C# script from script folder and drop in the inspector panel.
Save project
using UnityEngine;
using System.Collections;
public class CatchBox : MonoBehaviour {
int boxescollected =0;
int totalboxes =0;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
Lets add new method to check collision:using System.Collections;
public class CatchBox : MonoBehaviour {
int boxescollected =0;
int totalboxes =0;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
void OnCollisionEnter(Collision other)
{
if (other.gameObject.tag == "box") {
boxescollected += 1;
Destroy (other.gameObject);
}
}
Since the code is attached to "catch up", it detects collision between it and other game objects. We check if the object has tag "box" and add
boxescollected. We then destroy the box.
We need to display our boxes. We use OnGUI method
void OnGUI()
{
GUI.Label (new Rect (10,10, 300, 50), "Boxes collected: " + boxescollected);
}
Save the code.
Below is the complete code for our game:
using UnityEngine;
using System.Collections;
public class CatchBox : MonoBehaviour {
int boxescollected =0;
int totalboxes =0;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnCollisionEnter(Collision other)
{
if (other.gameObject.tag == "box") {
boxescollected += 1;
Destroy (other.gameObject);
}
}
void OnGUI()
{
GUI.Label (new Rect (10,10, 300, 50), "Boxes collected: " + boxescollected);
}
}
Run the game
This is the most basic games.
We learn the basics of working with unity. The game developed so far is a skeleton game. Only one box dropped and collected.
What we know:
Story line:
The game involves some boxes that drop from above and the player is required to collect the boxes. At the end of the game. the player should be able to tell how many boxes we generate and how many s/he managed to collect.
What we have done so far:
Dropped a box and collect it and, display the number of box collected . To further our game, we need to drop more boxes and collect them. This will enable us display the total boxes generated. We need to advance our story line:
Updated Story line:
The game involves some boxes that drop from above and the player is required to collect the boxes.The boxes drop from any point above the catch up box. We move the catch up box to collect the boxes on left or right. No box should fall down. At the end of the game. the player should be able to tell how many boxes we generate and how many s/he managed to collect.
The new story line provides:
What we know:
Story line:
The game involves some boxes that drop from above and the player is required to collect the boxes. At the end of the game. the player should be able to tell how many boxes we generate and how many s/he managed to collect.
What we have done so far:
Dropped a box and collect it and, display the number of box collected . To further our game, we need to drop more boxes and collect them. This will enable us display the total boxes generated. We need to advance our story line:
Updated Story line:
The game involves some boxes that drop from above and the player is required to collect the boxes.The boxes drop from any point above the catch up box. We move the catch up box to collect the boxes on left or right. No box should fall down. At the end of the game. the player should be able to tell how many boxes we generate and how many s/he managed to collect.
The new story line provides:
- We generate the boxes randomly.
- Move catch up box right or left. This now defines the player.
- Display the boxes lost.
- Display total boxes dropped
To make the game more interactive, we introduce the player element. We need to move the player to collect the boxes.
We first check if the total boxes collected are less than boxes to collect. If condition holds we drop a box and collect.
initialize boxcollected to zero
initialize totalboxes to zero
initialize boxtocollect to 40
While totalboxes < boxtocollect
drop box
Move playerbox
if collected
increase boxcollected by one
print boxcollected
end if
increase totalboxes by one
print totalboxes
end while
Our pseudo-code is taking shape now.
We first check if the total boxes collected are less than boxes to collect. If condition holds we drop a box and collect.
initialize boxcollected to zero
initialize totalboxes to zero
initialize boxtocollect to 40
While totalboxes < boxtocollect
drop box
Move playerbox
if collected
increase boxcollected by one
print boxcollected
end if
increase totalboxes by one
print totalboxes
end while
Our pseudo-code is taking shape now.
Just a reminder:
- You need to know your goal. Always remind yourself what your goal is a game developer. Whether it is to make money, entertain, train or for fun then don't deviate from that goal. Most of us give up as result of undefined goal.
- Review your skills. Get to understand what you need to develop your game. The skill needed to develop a game can be acquired from a training college, online, self taught or trial and error. Always expand your skills through self training and appreciate any little progress you make.
- Review your tools of work. Keep yourself up-to-date with new developments in regard to the technology you adapt to develop your game. Unity keeps on changing every day. You need to acquire new skills as they are released.
- Do not limit yourself to one field. Always explore to learn more.
- It is never too late to start. Even with saturated market, your game stands a chance. Proper marketing coupled with constant updates makes your game visible to the world.
- Marketing. You can develop a marketing plan. Either free or paid.
- Social media. Create online accounts on various social media and keep constant updates to keep you audience engaged.
- In-game marketing. When developing game, give link to your games or marketing images for your games.
- Create support channels for your games. e.g create a YouTube channel for cartoon, music etc and use it to market your games.
- Website. Create website for your game. Update it frequently.
No comments:
Post a Comment