ZombieHitman – Top-Down Shooter

The third game I made for my course was a top-down shooter genre prototype. For this Prototype, I used kenney.nl to get some assets to form the game with.

When looking through the assets for what to use, I saw a hitman character that I wanted to use for the game, and I paired it with the zombie model as enemies. I also added some crates and stones as defence against zombies. all the assets I used in the pack are below:

When I added these to the scene, I gave the player and enemy RigidBody components, which I froze their rotation with, and circle colliders while I gave the crates box colliders and the stones polygon colliders to fit their shape better.

When  it came to coding I first worked on the player’s Movement in the script scr_Player_Movement, which controls which way the character is looking, and how it moves around the scene

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Scr_Player_Movement : MonoBehaviour
{
    public float moveSpeed = 5f;

    public Rigidbody2D rb;
    public Camera cam;

    Vector2 movement;
    Vector2 mousePos;

    // Update is called once per frame
    void Update()
    {
        movement.x = Input.GetAxisRaw(“Horizontal”);
        movement.y = Input.GetAxisRaw(“Vertical”);

        mousePos = cam.ScreenToWorldPoint(Input.mousePosition);
    }

    void FixedUpdate()
    {
        rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);

        Vector2 lookDir = mousePosrb.position;
        float angle = Mathf.Atan2(lookDir.y, lookDir.x) * Mathf.Rad2Deg;
        rb.rotation = angle;
    }

}

In this script, the mouse cursor is followed, which is used to point towards where the character will turn to look. The line ‘Vector2 lookDir = mousePos – rb.position is used to determine where to look as it checks where the mouse is in relation to the player and determines an angle from there which sets the rotation to the correct angle.

Scr_PlayerShooting is also attached to the player and it controls how the player shoots their gun.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

 

public class scr_PlayerShooting : MonoBehaviour
{
    // Start is called before the first frame update

 

    public Transform bulletSpawnLocation;
    public GameObject bulletPrefab;

 

    public float bulletForce = 20f;

 

    // Update is called once per frame
    void Update()
    {
        if (Input.GetButtonDown(“Fire1”))
        {
            Shoot();
        }

 

    }
    void Shoot()
    {
        GameObject Bullet = Instantiate(bulletPrefab,
bulletSpawnLocation.position, bulletSpawnLocation.rotation);
        Rigidbody2D rb = Bullet.GetComponent<Rigidbody2D>();
        rb.AddForce(bulletSpawnLocation.right * bulletForce, ForceMode2D.Impulse);
    }
}

This script waits for the left mouse click to be pressed, and activates the shoot function. The shoot function creates a bullet, fetches the RigidBody component, and applies a force to the bullet to shoot it forward wherever the player is looking.

The next script is scr_Bullet which tells the bullet what to do when an enemy is hit.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Scr_Bullet : MonoBehaviour
{
    public GameObject enemyRespawnPrefab;

    void OnCollisionEnter2D(Collision2D collision)
    {
        if(collision.gameObject.tag == “Enemy”)
        {
            Destroy(collision.gameObject);
            Destroy(gameObject);
            Vector3 enemyRespawnPosition = new Vector3(Random.Range(-5,5),
Random.Range(-3,3), 0);
            Instantiate(enemyRespawnPrefab, enemyRespawnPosition,
Quaternion.identity);
            Destroy(gameObject);
        }
    }
}

This script detects when the bullet hits an enemy by using the colliders attached to each object. If it hits something tagged as an enemy, then it will destroy the bullet and the enemy, and spawn a new enemy somewhere on the map randomly using the Random.Range function I learned from the space invaders prototype.

There is one last script that is used, and it is scr_EnemyMovement.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class Scr_EnemyMovement : MonoBehaviour
{
    public float moveSpeed = 1f;
    private Rigidbody2D rb;

    private GameObject player;
    private Rigidbody2D playerRb;


    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        player = GameObject.Find(“obj_Player”);
        playerRb = player.GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        Vector2 lookDir = playerRb.positionrb.position;
        float angle = Mathf.Atan2(lookDir.y, lookDir.x) * Mathf.Rad2Deg;
        rb.rotation = angle;

        float step = moveSpeed * Time.deltaTime;
        rb.position = Vector2.MoveTowards(rb.position, playerRb.position, step);

    }
   
    void OnTriggerEnter2D(Collider2D other)
    {
        if (other.tag == “Player”)
        {
            SceneManager.LoadScene(SceneManager.GetActiveScene().name);
        }
    }
}

The script locates where the player is in the scene, and gets the enemy to look in their direction and move forward, very much like the player movement. Once the enemy collides with the player, it restarts the scene, which I learned how to do in the space invaders prototype, and you die.

Overall, I think that the game came out well, but it feels a bit dull, and therefore I think could go back sometime and add more to it. I think having more variety in enemy types, and different powerups would make it much more enjoyable. I would also implement a scoring system to give meaning to killing enemies to get a higher score