Unity Junior Programmer Mission: Create With code Unit 2 – Basic Gameplay

My Custom Layout in Unity Development Environment

In this mission I undertook the bold task to program a top-down game with the objective of throwing food to hungry animals. To make things even more outrageous, the wild hungry animals are stampeding toward you and you must feed them before they can run past you. While working on this I became much more familiar with some of the most important programming and Unity concepts, including if-then statements, random value generation, arrays, collision detection, prefabs, and instantiation. I programmed a “Spawn Manager” to handle the spawning of random animals at random intervals. By completing this project I have demonstrated the ability to program a basic game complete with launching projectiles and maneuvering the player to keep the game alive.

https://meta.dwdenney.com/unit2/

Project Outcomes:

The player will be able to move left and right on the screen based on the user’s left and right key presses, but will not be able to leave the play area on either side.

The player will be able to press the Spacebar and launch a projectile prefab into the scene, which destroys itself when it leaves the game’s boundaries. The animals will also be removed from the scene when they leave the game boundaries.

When the user presses the S key, a randomly selected animal will spawn at a random position at the top of the screen, walking towards the player.

The animals will spawn on a timed interval and walk down the screen, triggering a “Game Over” message if they make it past the player. If the player hits them with a projectile to feed them, they will be destroyed.

Project Milestones:

  • The player can press the Spacebar to launch a projectile prefab,
  • Projectile and Animals are removed from the scene if they leave the screen

New Concepts & Skills

  • Create Prefabs
  • Override Prefabs
  • Test for Key presses
  • Instantiate objects
  • Destroy objects
  • Else-if statements

New Functionality

  • The player can press the S to spawn an animal
  • Animal selection and spawn location are randomized
  • Camera projection (perspective/orthographic) selected

New Concepts & Skills

  • Spawn Manager
  • Arrays
  • Keycodes
  • Random generation
  • Local vs Global variables
  • Perspective vs Isometric projections

New Functionality

  • Animals spawn on a timed interval and walk down the screen
  • When animals get past the player, it triggers a “Game Over” message
  • If a projectile collides with an animal, both objects are removed

New Concepts & Skills

  • Create custom methods/functions
  • InvokeRepeating() to repeat code
  • Colliders and Triggers
  • Override functions
  • Log Debug messages to console
Spawn Manager C# Code Sample

public class SpawnManager : MonoBehaviour
{
    public GameObject[] animalPrefabs;
    private float spawnRangeX = 10;
    private float spawnPosZ = 20;

    //settings for spawn rate
    private float startDelay = 2;
    private float spawnInterval = 1.5f;

    // Start is called before the first frame update
    void Start()
    {
        InvokeRepeating("SpawnRandomAnimal", startDelay, spawnInterval);
    }

  

    //method to spawn random animals
    void SpawnRandomAnimal()
    {
        int animalIndex = Random.Range(0, animalPrefabs.Length);
        Vector3 spawnPos = new Vector3(Random.Range(-spawnRangeX, spawnRangeX), 0, spawnPosZ);
        Instantiate(animalPrefabs[animalIndex], spawnPos, animalPrefabs[animalIndex].transform.rotation);
    }
}

//declare custom variables
    public float horizontalInput;
    public float verticalInput;
    public float speed = 10.0f;
    public float xRange = 10;
    public GameObject projectilePrefab;
    
   
    // Update is called once per frame
    void Update()
    {
        //initialize the horizonal input varible based on the users input
        horizontalInput = Input.GetAxis("Horizontal");
        // move the player character based on the horizontal input
        transform.Translate(Vector3.right * horizontalInput * Time.deltaTime * speed);

        // check to keep the player in bounds
        if(transform.position.x < -10)
        {
            transform.position = new Vector3(-xRange, transform.position.y, transform.position.z);
        }
        if (transform.position.x > 10)
        {
            transform.position = new Vector3(xRange, transform.position.y, transform.position.z);
        }

        // catch the users keyboard input of "space bar" to fire the projectile
        if (Input.GetKeyDown(KeyCode.Space))
        {
            //launch a projectile
            Instantiate(projectilePrefab, transform.position, projectilePrefab.transform.rotation);
        }

    }

public class MoveForward : MonoBehaviour
{
    //declare custom variables
    public float speed;
    
    
    // Update is called once per frame
    void Update()
    {
        transform.Translate(Vector3.forward * Time.deltaTime * speed);
    }
}


public class DetectCollisions : MonoBehaviour
{
   

    void OnTriggerEnter(Collider other)
    {
        Destroy(other.gameObject);
        Destroy(gameObject);
    }
}


public class DestroyOutOfBounds : MonoBehaviour
{
    private float topBound = 30;
    private float bottomBound = -10;
    
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if(transform.position.z > topBound)
        {
            Destroy(gameObject);
        } else if (transform.position.z < bottomBound)
        {
            Destroy(gameObject);
            Debug.Log("Game Over!");
        }
    }
}


Leave a Reply