Sunday, October 5, 2014

Unity3D Stealth Game Enhancements


Bharath Kumar Bommana, Puja Davande, Logan Sales

These enhancements are made after the finishing the Unity3D Stealth Game Tutorial found here: https://unity3d.com/learn/tutorials/projects/stealth.

Level 2

We have added Level 2 in our Stealth project.

For Level 2 we have added terrain in our project. For terrain we downloaded asset from asset store and imported it in project. With the help of terrain tool we created hills and trees.


This is Level 2 scene.

Basically when player in lift, and lift goes up then second level gets started. After finishing level first successfully it will display the below message .


This is Level 2.


When Level 2 started player can sneak, he can run .

Implementing a User-Controlled Drone

To add the drone to the scene, first we need a drone asset. Fortunately there are a few free drone assets available in unity asset store. We downloaded and imported the drone asset “PA SciFi Enemies and Vehicles” from the below url: https://www.assetstore.unity3d.com/en/#!/content/15159 . The assets will be added into PopupAsylum folder in the assets in unity.

Add PA_Drone in the characters folder to the scene. set the position to (-3.1,0.8,0). Now add a camera to the scene and rename it to drone_camera and rotate the camera by 90 about x-axis. Add a tag, “secondary camera” to this gameobject. We want this camera to produce a bird’s eye view of the environment in the inset, in the game’s view. To make this we need to restrict this camera’s view to a small window as shown in the below screenshot.


Bird’s eye view:


We need to make the camera to move along with the drone, make the camera a child object of drone, so it could follow the drone.

Moving the Drone

The Horizontal and veritcal axis are used for the player movement. So, in order to move the drone along x,y,z axis, we need to define these axes with different keys.

Adding axes: Unity, EDIT → Project settings → Input, you will be directed to Input Manager in the inspector.

We need to define Axes in the input manager. The screenshot shows the movement along x-axis.


After adding the axes for x,y and z, we can use them for moving the drone using the following code.
void FixedUpdate () {
float x = Input.GetAxis("dronex");
float z = Input.GetAxis("droney");
float y = Input.GetAxis("dronez");

Vector3 droneMovement = new Vector3 (x, y, z);
rigidbody.velocity = droneMovement * Speed * Time.deltaTime;
}

rigidbody.velocity:

Rigidbody.velocity adds a big starting power to the objects and then it gets slower in the air due to the air drag. Rigidbody.velocity adds a constant force in a specific direction and it doesn't take into account the possible air drag. SInce we want the drone to be in total control and manually operated, we used Rigidbody.velocity to make it move.

We used keystrokes K and L to move the drone along Y- axis, H and J for X-axis and I and O for the Z-axis. You can use the keys of your choice by adding those keys while defining the new axes in the inspector.

Now that we can move the drone, we may also use the drone to assist the player in simple tasks like collecting the key to open the lift. This task can be implemented by adding a collider to the drone and write a simple script to detect the collision between key and drone.

Teleporting

When the player is surrounded by enemies, we often wish if we can vanish the player from his current position and appear in some other position. There can be many complex ways of doing it, but a simple implementation could be transforming his x-position by an offset(say 2 units) based on the keystrokes the user make. In our project, we used “space” key to move the player towards positive-X, and “keypad Enter” to move the player towards negative-X.

void TeleportPlayer() {
    if(Input.GetKeyDown(KeyCode.Space)) {
        Vector3 temp = new Vector3(3.0f,0,0);
        gameObject.transform.position += temp;
    } else if(Input.GetKeyDown(KeyCode.KeypadEnter)) {
        Vector3 temp = new Vector3(-3.0f,0,0);
        gameObject.transform.position += temp;
    }
}

CCTV Camera Viewing

You may want a way to “hack” into the CCTV cameras to see through them. This could be useful for monitoring enemy robot movements, get a better sense of the layout of the level, and to be more mindful of their placements.

To do this, first we can add a Camera to each CCTV Camera in the scene and it should be attached to the proc_cctvCam_body as shown.


We do this because we want the Cameras to match the rotation that we’ve already put on the body. Note that once you add the Camera to the CCTV body, you don’t want to Apply this addition to the prefab because each CCTV camera has a different rotation. You’ll notice that the turning animation applied to the camera at the t-intersection will also move with the camera body. If we create a new tag for these cameras, they can be found easier later on.

Now we a way to change from the main camera view to the CCTV camera view. We opted to add a simple interface to the laser deactivation terminals. To start this off you’ll want to create an empty game object that will serve as a controller for the CCTV cameras and switching between them.

The next thing is to add all of the new cameras to a structure we can iterate over, so create a script for the game object we created in the previous step. Since we tagged the cameras before, we can find them all in the Awake() function with the GameObject.FindGameObjectsWithTag() function. This can also be used to find the main camera, we’ll need it so we can deactivate it and activate the other cameras. When you find all the CCTV cameras and store them in an array, you will also want to disable them on awake so that the game doesn’t start and use one of those cameras instead of the main camera. This can be done by calling SetActive(false) on each camera in the array.

Now we might want some functions to iterate over the array to switch between which camera is active. Provided below is a function that will iterate over each camera in the order they are added.

void SwitchCameraForward () {
cctvCameras [currentCamera].SetActive (false);

previousCamera = currentCamera;
currentCamera = (currentCamera + 1) % cctvCameras.Length;

cctvCameras [currentCamera].SetActive (true);
}

Here we store the previous camera and the current camera in case we also want to iterate over them in reverse order. When dealing with array indices, we must always be mindful of the size of the array. That is is why when we increment the currentCamera index, we also use the modulus operator to loop back to the first index in the array.

So now when we hack into the camera, it can look something like this:


Manual Door Activation

To add more interaction with the scene for the player, we might want them to manually open the door by pressing a button on the keyboard. This is a very common feature in most video games and may or may not be preferable to always having automatic doors. Also, this can allow the doors to distinguish between the player and the enemy robots. For simplicity sake, we can use the “Switch” button, as it is already an input for the game.

Most of the logic for this will be modifying the DoorAnimation script and moving the player detection section from OnTriggerEnter() to OnTriggerStay(). This is because we want to check whether the player uses the “Switch” key while within the door collider.



Notice how we have a boolean value called hasPlayer. This is because we cannot use the count increment logic as with OnTriggerEnter(). If we did, count would continue to increase by one every frame the player remains within opening distance of the door, thus the door will never close even when the player exits the door collider. So now, when the player approaches the door, it will not open until they press the “Switch” key.



Two more additions we will need is to set hasPlayer to false when they leave the collider in OnTriggerExit and to update the Update() function so that anim.SetBool() will check if count > 0 or if it hasPlayer. The first modification lets the Update() function know that the door may automatically close and the second modification takes into account the modified player logic so it can use the appropriate animation.

No comments:

Post a Comment