Friday, October 3, 2014

Unity Game Tutorial – Stealth Modifications

                                                              Anicia Dcosta, Nirav Sharda, Swetha Naidu


This is a tutorial based on the Stealth game which is available on unity3d.com.The original game has a character named Ethan. His task is to exit the unity labs which has CCTV, Laser grids and enemies with guns to stop him. We have made some changes to that project and the help associated with implementing those changes is shown in this document.

We have made some modifications in this game as follows.

  • Jumping motion is been enabled for Ethan.
  • Providing Ethan with more chances to escape from the lab. Ethan has to pick rotating cubes (placed around in the lab) to increase his chances. Each cube provides one more chance for Ethan to navigate the simulation lab and can be thought of as a lifeline. Hence number of additional chances (lifelines) are equal to the number of cubes picked up. Number of lifelines left will be displayed in the left corner. If Ethan dies he will start playing from the starting position as long as he has additional lifelines.
  • Changed some laser grids to dragons.
  • Added a bonus item which when picked up, the character can move through the view of the CCTV camera without triggering alarm.

  1. Making the Player Jump :

    In order to make the player jump, the player needs to be attached a rigid body
and a force needs to be added using rigidbody.AddForce method. An additional
input named “Jump” is set with the key “j”. The code associated with the player jump is shown below.

Fig 1 : Code Snippet for making the player to jump.
Fig 2 :Making the player Jump


  1. Creation of the rotating cubes:
                 
               Fig 3: Rotating of Lifeline Cubes

Cubes will be placed at different locations in the scene. Currently they are placed in three locations and will be rotating. Cubes will have random colors. These cubes will be randomly appearing and disappearing. Below is the script for rotation of the cube and its random color generation.
        Fig 4 : Code snippet for Rotation and Appearing/Disappearing of the Lifeline cubes


  1. Picking up the cubes :

Once Ethan picks up the cube the number of lifelines available to him increases. The number of lifelines will also be displayed at the left corner of the scene. Picked up cubes will get destroyed.  Below is the code which will help us to pick up the cubes.
                Fig 5 : Code snippet for collecting the lifelines

Main Code for Lifeline:

Once Ethan gets shot by the guards if he has to become alive and come back in the game, we need to change the state diagram in player animator. We create a new parameter “Life” (bool). In state diagram, the player goes from dying state to locomotion state only when the parameter “Life” is true. Below is the state diagram.


                Fig 6 : State diagram of the Player

As long as Ethan has a lifeline we will make him invisible after he is dead. This can be done by setting the renderer to false. Also since Ethan has a lifeline expectation is he should be able to continue the game. Hence his position is set to the original position where he becomes visible again after a delay. The delay is achieved by using “Invoke” method. Each time Ethan dies his lifeline is decremented.
           Fig 7 :Code Snippet Showing Ethan get back Life and becoming Invisible

        Fig 8 :Code Snippet Showing Ethan becoming back Visible


  1. Adding bonus coin to disable CCTV:
   
                  Fig 9 : Bonus item with light on it

The bonus item has a blue light with it. If the character picks it up the alarm and light will not trigger when he passes through the CCTV camera. The code for it is explained below:

Fig 10 : Code snippet for collision detection with bonus item

When the player collides with the bonus item, we set a boolean coinpick to true and then destroy the gameobject i.e bonus item in this game. This boolean can be used to then disable the alarm.

Fig 11 : Code snippet for changing the collision detection code with CCTV

Now you have the boolean set as true when the bonus item is picked up, so you just check that in the if condition of collision detection code associated with the CCTV code.

  1. Adding Dragons as an Obstruction:

Fig  12 : Snapshot of dragons spitting fire instead of two laser grids.

The dragon used is a free asset and can be downloaded from the unity store. The fire is made using a particle system and has a fire texture associated with it. Help with particle systems is available online. So if the character goes through the fire without sneaking he dies. Since we cannot add a collider to particle system, we added a capsule collider to the dragon and changed its position to the fire.The code for this is shown below:
Fig 13 : Code snippet for collision with fire:

Fig  14 : Code snippet for allowing the player to burn:

The fig 14 shows the function written in playermovement class. It just sets a boolean based on whether the sneak key is pressed or not. If boolean burn is true it means that sneak key is not pressed and  if the player now collides with the capsule collider i.e. the fire, his health reduces to 0 as seen in figure 13. 


No comments:

Post a Comment