Select Page
Game Design
Class 10: FPS Game Mechanics

Topics

  • AI
  • First Person (game mechanics) Lab Demonstration
  • Enemy Design Assignment

Pefect 1-0

AI

Artificial Intelligence:

Artificial Intelligence is a system whereby a computer is programmed to dynamically recieve input and exectute a thoughful reaction, while learning from the experience.

Navmesh:

Unity has a built-in system called Navmesh that allows the engine to determine pathways for Navagents. 

First Person (game mechanics) Lab Demonstration

This is what we are making today

You may download the Unity project here.

Assets

Environment
Terrain
The ground that is sculpt through a heightmap
Trees/Details
Trees and other details such as rocks are placed on the terrain
Other assets
Other geometry is imported such as buildings and water. A skybox is imported
Game Controller
This will control the game for now it will spawn the enemies
Characters
Player
First person controller
Enemies
Zombies that will seek you out and damage you

We do not have finalized graphics but a good start.

Scripts

New Scripts
PlayerControllerScript
We will apply a shooting mechanic to this script
PlayerStatsScript
Holds health and may add or remove it
EnemyControllerScript
Directs the enemy towards the player, applies damage, and handles health
EnemySpawnScript
Will spawn the enemy wherever it is place at a rate given
PlayerController.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public float nextFire = 0.0f;
	public float fireRate = 0.5f;
	public float damageAmt = 25.0f;
	public float knockBackAmt = 250f;

	public Transform shootPos;

	public GameObject hitSpawn;
	EnemyController enemyController;

	public LineRenderer line;

	Ray shootRay;
	RaycastHit hit;
	bool shootResult;
	GameObject spawnedHit;

	void Update () 
	{
		if(Input.GetButton("Fire1") && Time.time > nextFire)
		{
			Shoot(shootPos.position, shootPos.forward);
			nextFire = Time.time + fireRate;
		}
	}

    void Shoot (Vector3 origin, Vector3 direction)
    {
		shootRay = new Ray (origin, direction);
		Debug.DrawRay (shootRay.origin, shootRay.direction * 3f, Color.red, 1f);

		line.enabled = true;

		line.SetPosition(0, new Vector3 (origin.x -.25f, origin.y -.25f, origin.z  + .1f));
		line.SetPosition(1, shootRay.GetPoint (100));

		StartCoroutine(ShootLine());

		shootResult = Physics.Raycast (shootRay, out hit, 50f);
		if (shootResult) 
		{
			spawnedHit = Instantiate (hitSpawn, hit.point, Quaternion.identity);
			StartCoroutine (ShootSpawn (spawnedHit));

			enemyController = hit.transform.GetComponent<EnemyController>();
			if(enemyController != null)
			{
				hit.transform.gameObject.GetComponent<Rigidbody>().AddForceAtPosition (shootRay.direction * knockBackAmt, hit.point);
				enemyController.EnemyHealthAdjustment(-damageAmt);
			}
		}						
	}

	IEnumerator ShootLine()
	{
		yield return new WaitForSeconds (.25f);
		line.enabled = false;
	}

	IEnumerator ShootSpawn(GameObject spawnedHit)
	{
		yield return new WaitForSeconds (2f);
		Destroy (spawnedHit);
	}
}
PlayerStats.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerStatsScript : MonoBehaviour
{
    public float playerHealth = 100f;

    public void PlayerHealthAdjustment(float playerHealthAdjustment)
    {
        playerHealth += playerHealthAdjustment;
    }
}
EnemyController.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyController : MonoBehaviour
{
    private GameObject playerObject;
    private UnityEngine.AI.NavMeshAgent enemyNav;

    private LayerMask playerMask = 1 << 8;
    public bool playerPresent;

    public float enemyHealth = 100f;

    public float enemyAttackAmount;
    public float enemyAttackRate;
    private float enemyNextAttackTime = 0.1f;

    void Awake()
    {

        playerObject = GameObject.FindGameObjectWithTag ("Player");
        enemyNav = GetComponent<UnityEngine.AI.NavMeshAgent>();
    }

	void Update () 
	{
        if(!playerPresent)
		    enemyNav.SetDestination (playerObject.transform.position);
	}

    void FixedUpdate()
    {
        playerPresent = DetectPlayer();
        AttackPlayer();
    }

    bool DetectPlayer()
    {
        Vector3 fwd = transform.TransformDirection(Vector3.forward);

        if (Physics.Raycast(transform.position, fwd, 1, playerMask))
            return true;
        else
            return false;
    }

    void AttackPlayer()
    {
        if(playerPresent && Time.time > enemyNextAttackTime)
        {
            enemyNextAttackTime = Time.time + enemyAttackRate;
            playerObject.GetComponent<PlayerStats>().PlayerHealthAdjustment(-enemyAttackAmount);
        }
    }

    public void EnemyHealthAdjustment(float enemyHealthAdjustment)
    {
        enemyHealth += enemyHealthAdjustment;
        if(enemyHealth <= 0)
        {
            Destroy(gameObject);
        }
    }
}
EnemySpawn.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemySpawnScript : MonoBehaviour 
{
	public GameObject enemy;

	public float spawnRate;
	private float nextSpawnTime;

	void Update () 
	{
		if(Time.time > nextSpawnTime)
		{
			nextSpawnTime = Time.time + spawnRate;
			Instantiate (enemy, transform.position, Quaternion.identity);
		}
	}
}

Enemy Design Assignment

Enemy Design:

This game is a first person game. That means that the enemy is the main thing the player will see besides the environment. In this lab you will make a rough illustration of your enemy (or enemies if you want to do more). You will be creating a 3D model in the next assignment so be sure to design something that you feel comfortable modeling. Once completed you will submit your image.

You will be graded on the following:
  • Lab Requirements
    • Techniques and processes covered in the instructional material is followed and implemented.
  • Creativity & Craftsmanship
    • Excellent design choices, novel & appealing, and solid clean caliber work.
Resources:
Assignment Video Tutorials
You may watch the tutorial videos below to help you complete your assignment.

Assignment Video Tutorials

Wait! Before you go!

Did you remember to?

  • Read through this webpage
  • Submit Enemy Design Assignment on Blackboard