In this section we will discover how to add NPCs and give them basic intelligence so that they can follow the player .
7 steps to find and design your next game idea
As you build-up your game development skills, you may often be on the look-out for your next game idea; often, this happens to be a difficult process; you may feel stuck first, wondering, how you will be able to find an idea that is both original and challenging for the players; so, in this post, I will cover a few principles that you can start to use to draft and start to design the key mechanics and goals for your game. Continue reading
Creating a Timer and Pausing The Game in Unity
In most of the games that you will create, you will, more than likely, include the concept of time. This may mean a timer indicating how much time remains before the player loses, or simply, the ability to pause the game.
Implementing a Timer
So first, let’s look at the following snippet example that implements a timer:
float time;
void Update()
{
time += Time.deltaTime;
float seconds, minutes, hours;
seconds = time % 60;
minutes = time / 60;
hours = time / 3600;
print ((int)hours + “:” + (int)minutes + “:” + (int)seconds);
}
In the previous code:
- We use the Update
- We calculate the time since the game started and save it in the variable called time.
- We then calculate the current seconds, minutes, and hours based on the variable time.
If you were to create a countdown, the principle would remain the same, except that the variable time would be initialized to a value that is greater than zero and then decreased every seconds until it reaches zero.
Pausing the Game
In Unity, it is very simple to pause or resume your game, using the following code:
Time.timeScale = 0;//time is paused
Time.timeScale = 1;//time is back to normal
So, you could for example, create two buttons and functions: one button (and its associated function) for pausing the game as well as one button (and its associated function) for resuming the game. You could then call one of these functions based on the button that the player has pressed.
Book Giveaway (April 2018)
Communicating between C# Scripts in Unity
When creating your game in Unity, most of your C# scripts will generally be linked to an object. Because these scripts include different types of information, you may sometimes need these scripts to communicate between each other.
Using attributes and modifying the Inspector
As you create your games, there will be times when you will want to control some parameters at run-time to perform tests. For example: you may want to be able to modify a variable inside the Inspector while the game is running by typing its value or by using a slider to make this easier. In both cases, you will need to modify the Inspector and the way data is presented within.
Thankfully, Unity makes it possible for you to perform these changes from your own C# script. The first thing we could do, is to make our variable public, as all public variables are available and modifiable in the Inspector.
Game Programming and C# Books Giveaway
Creating maintainable and reusable code
When you are coding your game, the size and structure of your code can rapidly become overwhelming over time, unless you have, from the beginning, set a defined strategy that you will follow to ensure that your code will grow in a way that is manageable. To achieve this goal, there are several ways that you can follow including a component-based structure.
Flash Sale – December 2017
Hey 🙂
To help you kick start 2018 in style, I am offering 70% discount on all my books until midnight this Friday.
Collision Detection in Unity Explained (i.e., Colliders, Triggers and Ray-casting)
In your games, you will often need to detect collisions between objects. This can be related to NPCs or objects that you will collect or interact with. For this purpose, you can use (and monitor) colliders. However, the way you manage collisions, and the methods to be called in this case, will highly depend on the type of colliders used (for example, colliders or triggers) as well as the object from which you want to detect the collision (for example, simple objects or character controllers).
This being said, you may want to be able to detect the presence of objects located far away from the player or the NPC. In this particular case, ray-casting may make more sense than collision or trigger detection.
So this tutorial will explain in great details how you can detect objects using a wide range of techniques such as colliders, triggers and ray-casting.