In this article I will explain the steps I’ve taken to implement a basic Spawn Manager to the Space Shooter Project by using a powerful method called Coroutines. By assigning the spawning enemies to a new parent we keep the hierarchy overview clean and prevent it from clustering. We’ll also learn how to create a reference to the Player Script in order to prevent the Spawn Manager from spawning enemies if the Player is dead.
By creating a Spawn Manager we can spawn our enemies automatically so we wouldn’t have to set them up manually in the Scene.
A Component is the Base Class for everything attached to GameObjects.
It defines every aspect of a GameObject and its behaviour. Basically any script attached to the GameObject compiles as a Type of Component.
Depending on various situations you’ll need to make use of Unity events for when physics collisions or triggers occur. An event allows you to execute your code based on what is currently happening in the project like for example interaction between GameObjects when they touch each other.
An instance of two or more records being assigned the same identifier or location in memory.
Unity differentiates two types of methods for controlling objects behaviour for when they interact or collide with each other.
OnCollisionEnter(Collision other) takes a Collision type parameter that for example returns us information on Contact points…
Physics in Unity ensure that objects accelerate and respond to collisions, gravity, and various other forces. Unity provides us with different physics engine implementations which you can use according to your Project needs.
Unity differentiates its physics engines under two categories.
-Built-in physics engine for object-oriented projects (2D/3D).
-Physics engine packages for data-oriented projects (DOTS).
For the space shooter project we will only use built-in 3D physics, the usage of the dedicated DOTS physics packages will be covered in a future article.
A few important concepts of the built-in 3D physics engine briefly explained.
Enables physical behaviour for a GameObject…
Let’s have a closer look at what we’ve been working with.
In C# we have a method provided by Unity to make a copy (clone) of an object, similar as if you would copy and paste an object in the scene. This method is very powerful and we’ll be using it for projectiles, particle systems, explosion effects and in many other situations where we need to create a new object at runtime.
By default Instantiate() clones the object “original” and returns the clone. However there are a few optional parameters that we can declare.
The position and orientation for…
Now that we have a mechanism to fire a Projectile and modified its behaviour, it is time to add something to destroy.
Very similar to how we made the Laser Prefab, we prepare a new Prefab for our Enemy.
In this function we assign a new position to our Enemy’s transform position, after having it multiplied with our speed and Time.deltaTime. In our case the direction is specified as downwards.
However, if the Enemy were to go out of the boundaries…
In the previous article you are introduced to Pseudocode by the hand of an example for the player to be able to fire a Laser, with a cool-down mechanism. Today, we’ll implement that feature in our code.
First we must create our Laser Prefab. Don’t worry about its graphics, that will be covered in another article.
Create the Laser Prefab
→ Create a new 3D Object Capsule > rename it to ‘Laser’. > Adjust the Transform Scale to (0.1, 0.15, 0.1).
→ Create a new folder in the Project window called “Prefabs” and drag in the Laser to create a…
Often there are many ways to achieve the desired result through code. The possibilities are endless and that makes programming exciting although not always as easy, a heads up; “it can get messy”.
To help us organise and plan a script we can break it down into simple parts in a language that everyone understands and is independent to any programming language, it can also help to share our idea behind our coding logic and can help to spot flaws or errors early on.
Comment out a line by adding // at the beginning of each line. …
Variables are the building blocks of programming with C# in Unity.
A Variable is like a box containing information by the hand of parameters that you set up.
A variable can either be public or private.
-public variables are accessible by any class and are visible for everyone in the inspector.
-private variables are only accessible by referencing through this class or struct.
There are 2 main types of variables that are used in C# Unity.
— Value Types, variables that are stored in a pre-defined location in memory.
Some examples are:
int stores integer values (whole numbers) = 1, 4…
Simple player movement in Unity
In this article you’ll learn about adding simple player movement to the GameObject, adding a toggle to swap between restricting or wrapping player bounds and as a little extra to apply a boost to our player.
Before we add the logic for the player to move, let’s open up a new script in Unity.
Add a Cube to your scene and make sure you have selected it. Hit the “Component” button in the inspector and add a new script called “Player”. When you open the script it will look like this by default.