Session 5

Zombie first person shooter

From the Unity Editor window, click on Window and open the Unity Asset store. For this tutorial, we are going to use a free Zombie model that has a couple of built-in animations. In the 3D Models section, search for “zombie” and “free”, and select the one made by Pxltiger.

Creating our own first person controller

Create a capsule, renaming it, and adjusting the settings for scale. Turn the mesh render off. Drag the main camera on top of this capsule to parent it. Modify the position of the main camera so it appears to be looking where the head would be while standing. Create a new C# script file named BasicFirstPerson and add the following script to it:

 using UnityEngine;
 using System.Collections;
 
 public class BasicFirstPerson : MonoBehaviour {
 
     public float moveSpeed;
     public float mouseSensitivity;
     public GameObject bulletPrefab;
     public Rigidbody rb;
 
 
     // Use this for initialization
     void Start () {
         rb = gameObject.GetComponent<Rigidbody>();
     }
     
     // Update is called once per frame
     void Update () {
         //Rotation
         float rotateHorizontal = Input.GetAxis("Mouse X") * mouseSensitivity;
         transform.Rotate(0, rotateHorizontal, 0);
 
         float rotateVertical = Input.GetAxis("Mouse Y")  * mouseSensitivity;
 
         float currentRotation = Camera.main.transform.rotation.x;
 
         Camera.main.transform.Rotate(-rotateVertical, 0, 0);
 
 
         if (currentRotation > 40) {
             Camera.main.transform.rotation.eulerAngles.Set(40, 0, 0);
         }
         if (currentRotation < -40) {
             Camera.main.transform.rotation.eulerAngles.Set(-40, 0, 0);
         }
 
 
 
         // Movement
         Vector3 moveDirection = new Vector3 (000);
 
         Vector3 vertMove = Vector3.zero; // Create variables to store the movement directions
         Vector3 horzMove = Vector3.zero;
 
         if (Input.GetAxis("Vertical") != 0) {
             vertMove = Input.GetAxis("Vertical") * transform.forward;
         }
         if (Input.GetAxis("Horizontal") != 0) {
             horzMove = Input.GetAxis("Horizontal") * transform.right;
         }
 
         moveDirection = horzMove + vertMove; // Combine movement directions
 
         moveDirection = moveDirection.normalized * moveSpeed; // Adding vectors together
 
         rb.velocity = moveDirection;

         if (Input.GetButtonDown("Jump")) {             Shoot();         }     }     void Shoot () {         Instantiate(bulletPrefab, transform.position, transform.rotation);     }              public void TakeDamage (string DamageType) {         if (DamageType == "zombie") {             Debug.Log("You've been hit by a zombie!");         }     } } 

Updated code in green

Just like our 2D space shooter, we’re going to make a bullet prefab. A sphere-like shape tagged “bullet”, and with Is Trigger on. Create the following script and attach it to the bullet. Click and drag the name of the bullet from the Hierarchy window to the Project window, just like the bullet for the vertical shooter, to create a prefab, and add the prefab to the bulletPrefab slot on the BasicFirstPerson controller.

 using UnityEngine;
 using System.Collections;
 
 public class bulletMotion : MonoBehaviour {
 
     public float bulletSpeed;
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         transform.Translate(0, 0, bulletSpeed * Time.deltaTime);
     }
 }
 

Mecanim

Covered during class session

Scripting the zombie

Place the Zombie model in the scene, and under Add Component > New Script, create Zombie.cs, and open it in MonoDevelop.

 using UnityEngine;
 using System.Collections;
 
 public class Zombie : MonoBehaviour {
 
     public Animator anim;
 
     public Transform player;
 
     public float moveSpeed;
     public float attackRange = 2.0f;
 
     public int health = 100;
     public int bulletDamage = 50;
 
     bool isAlive = true;
     bool attacking = false;
 
     public AudioSource source;
     public AudioClip moan;
 
     public Rigidbody rb;
 
     // Use this for initialization
     void Start () {
         rb = gameObject.GetComponent<Rigidbody>();
     }
     
     // Update is called once per frame
     void Update () {
         if (isAlive == true) {
 
             Vector3 moveDirection = player.position - this.transform.position;
             moveDirection = moveDirection.normalized * moveSpeed;
 
             float step = moveSpeed * Time.deltaTime;
             Vector3 newDir = Vector3.RotateTowards(transform.forward, moveDirection, step, 0.0F);
             Debug.DrawRay(transform.position, newDir, Color.red);
             transform.rotation = Quaternion.LookRotation(newDir);
 
 
             //transform.Translate(moveDirection);
 
             rb.velocity = moveDirection;
 
             float proximity = Vector3.Distance(this.transform.position, player.position);
             if (proximity < attackRange && attacking == false) {
                 StartCoroutine("Attack");
             }
         }
 
     }
 
     void OnTriggerEnter (Collider other) {
         if (isAlive == true) {
             if (other.CompareTag("bullet")) {
                 TakeDamage(bulletDamage);
                 Destroy(other.gameObject);
             }
         }
     }
 
     void TakeDamage (int damage) {
         health -= damage;
         if (health <= 0) {
             isAlive = false;
             source.PlayOneShot(moan);
             anim.SetTrigger("dead");
         }
     }
 
     IEnumerator Attack () {
         attacking = true;
         anim.SetTrigger("attack");
         player.GetComponent<BasicFirstPerson>().TakeDamage("zombie");
         yield return new WaitForSeconds(1.0f);
         attacking = false;
     }
 }
  

After adding the script, click and drag the Player capsule from the Heirarchy window to the Player (Transform) box in the Inspector on the Zombie script component. Adjust the move speed in the inspector to a low number like 2 so the zombie looks like it’s shambling toward you slowly.