The obstacle will consist of two parts. There will be an image of an axe that we will rotate around and a base image that the axe will rotate around. The reason we will be controlling the rotation of the axe instead of using another animation is that it would be more complicated to scale Box Collider 2D on the object to always fit the rotating bounds of the axe. Perform the following steps:
Assets
folder, right-click on it, select Create and then Folder.Obstacle
and open this folder.Obstacle
folder and click on Import New Asset….Art
files for the book and open the ChapterThree_Obstacle
folder.Obstacle_Axe.png
and click on Import.Obstacle
folder again and click on Import New Asset….Obstacle_Axe_Base.png
and click on Import.Obstacle
folder (click on one and Ctrl + left-click on the other).256
.Obstacle
folder in the scene. (If character and/or pickup are in the way, move them to the left or right direction so that you have room to work).0
, Y as 0
, and Z as 0
.0.1
.With Obstacle_Axe GameObject selected, navigate to the Inspector tab and click on Add Component. Search for Box Collider 2D and select Box Collider 2D.
0.12
.0.36
and Y as 0.2
.Axe
and set its Position to X0, Y0, and Z0.If you are having an issue where the pivot isn't at the center of Obstacle_Axe_Base, change the pivot position from Object to Pivot, as shown in the following screenshot:
You should now have GameObject in the Hierarchy tab named as Axe with three children: Obstacle_Axe_Base, Pivot, and Obstacle_Axe:
You should now be able to select the Pivot GameObject and rotate it around so that it looks as if Axe is rotating from the center of the Axe GameObject:
We now need to create the code class that will handle this obstacle. In the Assets/Scripts
folder, right-click and select Create and then C# Script. Name the class Obstacle
.
Select Axe GameObject in the Hierarchy tab. Now, in the Inspector tab, click on Add Component. Search for Obstacle and select it.
In the Assets/Scripts
folder, double-click on the Obstacle
class to open it. As before, there is the Unity-generated code.
At the top of the class, we need a reference to the Pivot GameObject so that we know what GameObject to rotate. Add the following code:
// Pivot to rotate around public Transform Pivot;
We also need the setting to set the rotation. We will use Vector2
and use it as a range. Add the following code under the Pivot
variable:
// Random range for rotation speed public Vector2 RotationSpeedGap;
As Vector2
is a struct with two float values, we can use both of its values for something other than a location. In this case, we will use them as the minimum and maximum rotation speed.
Now, we need a value that is chosen by RotationSpeedGap
. Under RotationSpeedGap
, add the following code:
// Actual rotation speed based on RotationSpeedGap private float RotationSpeed;
The concept here is that we can use the Random tool of the Unity engine to pick a value for us to avoid having absolute consistency between all obstacles. This way, each obstacle used will have its own rotation speed, making the game a bit more random and also a bit more difficult.
In the Start
function, add the following code:
// Use this for initialization void Start () { RotationSpeed = Random.Range( RotationSpeedGap.x, RotationSpeedGap.y ); }
This sets RotationSpeed
to a random value in the range that starts with RotationSpeedGap.x
and ends with RotationSpeedGap.y
.
Now, we have to rotate the Pivot
object based on the rotation speed in the Update
function. Add the following code:
// Update is called once per frame void Update () { Pivot.transform.Rotate( Vector3.forward, RotationSpeed ); }
The Rotate
function is designed to take a direction, which is Vector3.forward
and a speed to rotate around, which is our generated RotationSpeed
from the Random value between RotationSpeedGap.x
and RotationSpeedGap.y
.
Select Axe GameObject in the Hierarchy tab, and in the Obstacle component settings, click on Pivot and drag it onto the Pivot box of the component in the Inspector tab.
For the Rotation Speed Gap values, I will use X as 2
and Y as 5
. You can use anything you'd like; the higher the values, the faster the axe spins.
If you play the game now, you will see the axe spinning towards the player.
The last step of Obstacle is to write the logic that will check for the character colliding with it and then kill the character. The downside to how we have the Axe obstacle set up is that as the parent object doesn't know of Box Collider 2D on Obstacle_Axe, we have to create another C# class for Obstacle_Axe so that we can tell when the character collides with it.
In the Assets/Scripts
folder, right-click and select Create and then C# Script.
Name this Obstacle_Axe
.
Select the Obstacle_Axe GameObject in the Hierarchy tab, and in the Inspector tab, click on Add Component.
Search for Obstacle_Axe and select Obstacle_Axe.
Double-click on the Obstacle_Axe
C# script to open it.
Select the Start
and Update
functions and delete them. These functions are not needed.
The Obstacle_Axe
C# script should look similar to the following code:
using UnityEngine; using System.Collections; public class Obstacle_Axe : MonoBehaviour { // When our collider collides with another collider void OnTriggerEnter2D(Collider2D Col) { Character gameCharacter; if (Col.gameObject.name == "Character") { gameCharacter = Col.gameObject.GetComponent<Character>( ); if (gameCharacter != null) { gameCharacter.KillCharacter( ); } } } }
Similar to the pickup's OnTriggerEnter2D
function, we will check whether the GameObject owns the Col collider named Character. If it does, we get the Character
component on it and make sure it exists. If it does not, we will call the KillCharacter
function to kill the character.
If you play the game now, you can collide and be killed with Obstacle. Remember that there is no reset to revive the character yet. We will do this in the future UI chapter, where we will have the button to restart the game.