Creating the obstacle

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:

  1. To begin with, navigate to the Assets folder, right-click on it, select Create and then Folder.
  2. Name the folder Obstacle and open this folder.
  3. Now, right-click on the Obstacle folder and click on Import New Asset….
  4. Navigate to the Art files for the book and open the ChapterThree_Obstacle folder.
  5. Then, select Obstacle_Axe.png and click on Import.
  6. Now, right-click on the Obstacle folder again and click on Import New Asset….
  7. Select Obstacle_Axe_Base.png and click on Import.
  8. Select both the imported files in the Obstacle folder (click on one and Ctrl + left-click on the other).
  9. Now, select Filter Mode as Point.
  10. Then, select Max Size as 256.
  11. Now, select Format as Truecolor.
  12. Then, click on Apply.
  13. Let's click and drag each of these files onto the 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).
  14. Now, select Obstacle_Axe and Obstacle_Axe_Base GameObjects in the scene.
  15. In Inspector, select both of their positions for X as 0, Y as 0, and Z as 0.
  16. For Obstacle_Axe_Base, select its Z value as 0.1.
  17. Select Obstacle_Axe and move it up so that the base of the axe handle is in the center of Obstacle_Axe_Base, as shown in the following image:
    Creating the obstacle

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.

  1. In the Box Collider 2D settings, check IsTrigger.
  2. Also, in the Box Collider 2D settings, select the offset in the Y value as 0.12.
  3. Again, in the Box Collider 2D settings, select Size of X as 0.36 and Y as 0.2.
  4. Then, right-click on the Hierarchy tab and select Create Empty.
  5. In the Inspector window, name the new GameObject as Axe and set its Position to X0, Y0, and Z0.
  6. Select Obstacle_Axe and Obstacle_Axe_Base GameObjects and drag them onto the Axe GameObject.
  7. Right-click on the Hierarchy tab and select Create Empty again.
  8. Rename this new GameObject in the Inspector window as Pivot and set its Position to X0, Y0, and Z0.
  9. Now, move the Obstacle_Axe GameObject to the Pivot GameObject.

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:

Creating the obstacle

You should now have GameObject in the Hierarchy tab named as Axe with three children: Obstacle_Axe_Base, Pivot, and Obstacle_Axe:

Creating the obstacle

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:

Creating the obstacle

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.

Creating the obstacle

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset