Unity — Dissecting a Completed Minigame (Part 1)
Armed with an understanding of how to use the very basics of unity; like rolling a ball around on a plane. We are now able to delve into some slightly more complicated and difficult topics. The easiest way to explain some of these concepts is to dissect them in action. In order to do this I will be using a mini-game I have built using the Unity “Space-Shooter” tutorial, which I will include a link to at the bottom of the blog. Keep in mind this post is not a copy of that tutorial, but rather using it to explain concepts like Triggers, collision, and lighting
We will start off by looking at different ways to use lighting. It is important to note that the sun, or a single overhead light should not (except in certain circumstances) be your only light source. Things often look a lot better if you use different kinds of light sources to hide flaws in your models, as well as accentuate strengths.
In the above screen shot, we have a ship model with a flat background. Our attention should be payed to the lighting on the ship, and how it is so dark on the left side of it… This is because we have a light source shining onto the ship from the right, nicely lighting up the right side of it, but leaving the left to blend into the background. We could simply have the light shine straight down onto the ship, but that would make the entire ship have the same style of lighting and ruin the feel that there is a nearby star shining light onto the ship.
The best way to add some style to the ships model is to create a “Fill” and a “Rim” light. In order to properly light the other side of the ship as well as help show the player where the model ends.
With the added light sources, the ship is nicely shaded and entirely visible. So now the ships model is fully utilized and looks presentable, and the player is able to see the entire ship at once.
Next we will look into collides, and causing collisions between objects using Unities built in physics. In the last blog post, we created a ball object and applied a rigidbody to it, without that rigidbody… The ball would not have responded to physics collisions. This applies to all models and objects you create within unity. However, what we did not touch on is that an object that is given a rigidbody, is also given a Collider, the collider is the part of the object that actually responds to physics and other objects, then applies what it is given to the rest of the object. In our previous blog post, the ball was a default Unity shape, and already had a collider that fit it perfectly.
But our ship, which is a model downloaded through the Unity Asset store, is not a default object; and must be given a suitable collider. To do this, all we need to is click on the “Add Component” button we used to add scripts and the rigidbody object, select the “physics” option, and choose a collider. For our ship, we want to select a mesh collider. Which will create a collider that fits to the mesh of the ship model.
However, there is an issue presented by using this collider. Although the collider is going to be able to perfectly detect a collision on our ship, it is also needlessly complicated. The engine is going to have to check each triangle on our ship collider each and every frame in order to determine if one has collided with another object. This is not required in a game of this magnitude, and can be scaled back by creating a simpler model that resembles the main ship model. Then clicking on edit icon next to the “mesh” title under the “Mesh Collider” component, this will apply a much simpler collider to the ship that will do just as good of job while using less CPU.
Now we have a completed collider for our ship, allowing it to react to collisions with other objects and react to physics forces. The “Space-Shooter” tutorial has many more objects that colliders need to be applied too, and the other kinds of colliders can and should be used in other cases. Using the other colliders is all covered in the Space shooter tutorial.
There is another option within colliders that deserves plenty of attention in and of itself. And that is the “Is Trigger” option on all collider components. A trigger can be classified as a collider because the engine needs to make the same calculations to determine if a trigger has been entered or not. The main difference is that a trigger is used to determine if something has happened within the game world, and not all triggers are may necessarily act as physics objects. Examples of this include determining when a player walks through a certain doorway, you would detect this with an invisible trigger object placed in the doorway that the player can walk through. Another example includes detecting when a laser shot hits an enemy.
In Unity, for a collider to follow the rules of physics. the object must also have a rigidbody component, otherwise it will simply be passed or pass through other objects. If an object is a trigger, you can have it still “collide” with other objects while not using physics, allowing for you to detect when certain things are happening within the game.
In the next post I will be shedding some light on how to use colliders, and triggers from a coding standpoint in order to make a game act more like a game. From creating boundaries to destroying enemy ships when we fire upon them.