Intro

Welcome back to Chicken Time! Chicken Time is a simple mobile game reminiscent of Snake

We’ll be posting a series of tutorials every weekday! At the end of the series, you’ll have your own version of Chicken Time that you can play on any Android mobile device 😉 

Missed the first part of this tutorial series? You can check it out right here

_________________________________________________________________________________________________________

If you want to jump right into this tutorial without having to set up your own environment, or you just want to follow exactly what we did, go ahead and import this package into your project. It contains all the assets you’ll need, along with a ready-to-use scene!

Note: Import the Post-Processing package using the Unity Package Manager after importing the aforementioned package.

A picture showing where to enable preview packages in Unity.

If you can’t find it, make sure that “Show preview packages” is enabled!

In today’s tutorial, we’ll be covering how to collect eggs, keep track of them and win the game. Let’s start!

_________________________________________________________________________________________________________

Step 1: Add colliders

Add a capsule collider to your egg, any one in the scene will do. Make sure to set ‘Is Trigger’ to true, so the eggs won’t have a physical collider that interferes with your chicken. Here’s a quick rundown of how to do it.

A picture showing how to add a capsule collider as a component.    A picture showing how to enable ‘Is Trigger’.

Remember to apply it to the egg prefab.

 A picture showing how to apply the component to the prefab.

Step 2: Add the ‘Player’ tag

Next, add the ‘Player’ tag to the chicken. Here’s how you can do it.

A picture showing how to change the tag of the chicken.

Step 3: Collecting Eggs!

Now it’s time to add in the script for the chicken to collect and keep track of the eggs.

If you have imported the Unity Package, edit the script ‘ChickenStats’. It can be found under Assets > Scripts.

A picture showing where to find the script.

We use Visual Studio Community 2019 to edit code, you can download it here!

If you have not imported the Unity Package,

  1. Create a script called ‘ChickenStats’’.
  2. Create these variables at the start of your script.

A picture showing the variables to add in the script.

You can copy them here:

//Variables

[Range(0f, 20f)] public int numEggsToWin = 5;

 

[HideInInspector] public static int eggsCollected = 0;

 

[SerializeField] private GameObject door;

[SerializeField] private Vector3 doorFinalPos;

 

private bool hasCoroutineRun = false;

 

Add the script to your chicken and write this method to collect the eggs.

A picture showing the method to write.

You can copy it here:

//method to be called when the chicken collides with an egg

public void CollectEgg(GameObject eggObject)

{

    //disable eggObject in hierarchy

    eggObject.SetActive(false);

 

    //remove egg from total eggs active count

    SpawnEggs.totalEggs–;

 

    //increase egg count

    eggsCollected++;

    Debug.Log(“eggs collected: ” + eggsCollected);

}

Step 4: Egg Collider Script

If you have imported the unity package, edit the ‘EggColliderListener’ script. You can find it under Assets > Scripts.

A picture showing where to find the script.

If you have not imported the unity package, create a script called ‘EggColliderListener’.

Add this script to all three of your eggs. Remember to apply the script to the egg prefab!

A picture showing how to apply the component to the prefab.

For this Script, we’ll be using the built-in Unity method. We use this method to detect whenever our player is in the trigger zone of the egg, and get them to collect it. You can read more about it here. Write in this method.

 

Alt text: A picture showing the method to write.Feel free to copy it here:

//Variables

[SerializeField]

private string playerTag = “Player”;

 

//when something enters the egg’s trigger zone, check if it is the player character

private void OnTriggerEnter(Collider other)

{

if (other.gameObject.CompareTag(playerTag))

{

//call the collect egg method on the chicken

other.gameObject.GetComponent<ChickenStats>().CollectEgg(gameObject);

}

}

Step 5: Win Condition

Let’s provide a way for our chicken to escape the environment! We’ll lower the door and let the chicken escape once it has collected a certain amount of eggs.

First, let’s write a method that opens the door, this method will cover the animation of the door lowering.

Edit the ‘ChickenStats’ script, and include this method.

A picture showing the method to write.

Vector3.Lerp is used to move an object from one position to another smoothly, so it doesn’t just shoot to that position abruptly and looks more natural. Take a look at the official documentation here if you’d like to find out more!

You can copy the method here:

//method to call when game is won

IEnumerator GameWon()

{

//get the door’s start position

Vector3 doorStartPos = door.transform.position;

float i = 0;

 

//open the door

while (door.transform.position.y > doorFinalPos.y)

{

door.transform.position = Vector3.Lerp(doorStartPos, doorFinalPos, i / 10);

 

i += 0.1f;

yield return null;

}

}

In the Unity Editor, drag and drop the front door of the restaurant from the hierarchy into the ‘Door’ field in the ChickenStats component.

Note: If you have imported the Unity package, this door is called ‘Wall_Front_Centre’.

A picture showing what to assign the door variable as.

Next, we have to determine what the final position of the door should be. This will be when the door is open for the chicken to escape. Key in the coordinates of its final position into the ‘Door Final Pos’ variable. Here’s what our coordinates look like.

A picture showing the coordinates of the ‘Door Final Pos’ variable.

If you have imported the Unity Package and are using the scene we’ve provided, make sure that the final position of the door is low enough that the invisible collider will not still block the chicken from its escape after the door has been lowered. Here’s a closer look.

A picture showing the door lowered with the collider not blocking the chicken.

Now, back to the script. Let’s ensure that the method we have written is called when the win conditions have been met. In this case, we will check if enough eggs have been collected. This will be done in the Update() method, which basically runs every frame. Do take a look at the documentation here if you’d like to find out more.

Write this script in the Update method.

A picture showing the script to write in the Update method.

Go ahead and copy it here:

//check if the winning conditions have been met

private void Update()

{

if(eggsCollected >= numEggsToWin && !hasCoroutineRun)

{

hasCoroutineRun = true;

StartCoroutine(“GameWon”);

}

}

Tip: You can change the number of eggs the chicken has to collect to win by changing the ‘Num Eggs To Win’ slider in the inspector.

A picture showing the slider in the ‘Num Eggs To Win’ variable.

And that’s it for this tutorial!

Hooray, our chicken can now collect the eggs and has a way to escape from the environment!

Stay tuned for tomorrow’s tutorial, where we’ll implement chicks, checks and death! <3

_________________________________________________________________________________________________________

If you’re interested in finding out more about the game dev process, do follow our Instagram page @noodletsstudio! Not only will we be posting updates on the game we’re developing, we’ve also been putting up handy dandy tutorials on different aspects of game development. Currently, we have some tutorials on simple particle effects and mobile joystick movement up! Do check us out 😉

 

About Amphibi Studio

Amphibi Studio is studio led by students dedicated to creating explosive fun, emergent gameplay productions.

Mentors provide real-life guidance to cultivate students in creating their own games, interactive works or animation productions.

Blk 31, Level 7
School of InfoComm Technology
Ngee Ann Polytechnic
Singapore
E: info@amphibistudio.sg