Search
  • Charles Low

Food Flight!


This is a little game where the player:

  • Moves within a boxed boundary;

  • Needs to throw food at incoming dogs that are randomly spawned;

  • Successfully throwing food at a dog will make the dog disappear; and

  • Player can switch cameras.


PLAYER CONTROL

================


public class PlayerController : MonoBehaviour
{
    // Variable for forward movement speed
    public float speed = 20.0f;

    // Variable for movement input
    private float horizontalInput;
    private float forwardInput;
    
    // Variable for movement boundaries on x-axis
    public float xBound = 23.0f;

    // Variable for movement boundaries on z-axis
    private float zBoundTop = 30.0f;
    private float zBoundBottom = -14.0f;

    // Variable for food projectile
    public GameObject projectile;
    public Vector3 offset = new Vector3(0, 1.5f, 0);

    // Variable for cameras
    public Camera camera1;
    public Camera camera2;

    // Start is called before the first frame update
    void Start()
    {
    }

    // Update is called once per frame
    void Update()
    {
        // This is for player movement input
        horizontalInput = Input.GetAxis("Horizontal");
        forwardInput = Input.GetAxis("Vertical");
        transform.Translate(Vector3.forward * Time.deltaTime * forwardInput * speed);
        transform.Translate(Vector3.right * Time.deltaTime * horizontalInput * speed);

        // This is to set the boundaries to player movement on the x-axis
        if (transform.position.x < -xBound)
        {
            // If the player goes to the bound limits, the player will be moved to a position that is equal to the bound limit
            transform.position = new Vector3(-xBound, transform.position.y, transform.position.z);
        }

        if (transform.position.x > xBound)

        {
            // If the player goes to the bound limits, the player will be moved to a position that is equal to the bound limit
            transform.position = new Vector3(xBound, transform.position.y, transform.position.z);

        }
        // This is to set the boundaries to player movement on the z-axis
        if (transform.position.z < zBoundBottom)
        {

            transform.position = new Vector3(transform.position.x, transform.position.y, zBoundBottom);
        }
        else if (transform.position.z > zBoundTop)
        {
            transform.position = new Vector3(transform.position.x, transform.position.y, zBoundTop);

        }

        if (Input.GetKeyDown(KeyCode.Space))
        {

            // Instantiate (instantiate the projectile, instantiate from player position, rotation to be taken from the projectile object itself) 
            Instantiate(projectile, transform.position + offset, projectile.transform.rotation);


        }
        //This is for camera switch controls
        if (Input.GetKeyDown(KeyCode.P))

        {
            camera1.enabled = !camera1.enabled;
            camera2.enabled = !camera2.enabled;
        }
    }
}

FOOD FLIGHT

============


public class FoodMovement : MonoBehaviour
{
    // Variable for flight speed
    public float moveSpeed = 20.0f;
    // Variable for spin speed of food
    public float spinSpeed = 20.0f;
    // Variable for how far food will travel before being destroyed
    public float zLimit = 33.0f;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        // +=, e.g. x += means x = x + y. So in this case, it means transform.position = transform.position of object plus Vector3.forward * time * speed.
        // Vector3.forward = Vector3(0, 0, 1)
        // Under the Update() method, Vector3 will increase by 1 on the z-axis per second (with Time.deltaTime)
        // Alternatively could use transform.Translate, but this won't work with a spinning projectile.
        transform.position += Vector3.forward * Time.deltaTime * moveSpeed;
        transform.Rotate(Vector3.up * Time.deltaTime * spinSpeed);

        if (transform.position.z > 30.0f)

        {
            Destroy(gameObject);
        }
    }
}

ANIMAL MOVEMENT

=================



public class AnimalMove : MonoBehaviour
{
    public float speed = 20.0f;
    public float zLimit = -18.0f;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        transform.Translate(Vector3.forward * Time.deltaTime * speed);
        if (transform.position.z < zLimit)
        {
            // If the animal's position on the z-axis reaches the zLimit, it will be destroyed and the game will be declared over
            Destroy(gameObject);
            Debug.Log("Game Over!");
        }
    }
}


SPAWN MANAGER

===============


public class SpawnManager : MonoBehaviour
{
    // Declare a GameObject variable that is an array - animalPrefabs is a variable for a number of objects
    public GameObject[] animalPrefabs;
    public float xRange = 20.0f;
    public float spawnPosZ = 33.0f;

    // Start is called before the first frame update
    void Start()
    {
        // Repeat the calling of the function called SpawnRandomAnimal(), start one second after the game starts, repeat instantiation ever o.5 seconds
        InvokeRepeating("SpawnRandomAnimal", 1, 0.5f);
    }

    // Update is called once per frame
    void Update()
    {
    }

    // Void means that this function does not return anything - it just executes a code.
    void SpawnRandomAnimal()
    {
        // animalIndex is an integer but will be an integer within a range of integers
        // The range will be between 0 and the total length of the animalPrefabs array
        // Note that if you enter a number for the end-range integer, it will count one less. e.g. (0, 4) will mean a range of 0 to 3
        int animalIndex = Random.Range(0, animalPrefabs.Length);
        // the position at which the animals will spawn will occur within a Vector3, which includes a range for the x-axis and the variable spawnPosZ which is set to 33 on the z-axis
        Vector3 spawnPos = new Vector3(Random.Range(-xRange, xRange), 0, spawnPosZ);
        // Instantiate(object, which in this case is an array, at the spawn position Vector3 above, and facing the rotation based on the transform of the prefab)
        Instantiate(animalPrefabs[animalIndex], spawnPos, animalPrefabs[animalIndex].transform.rotation);
    }
}


DESTROY ON COLLISION

=====================


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


CAMERA FOLLOW PLAYER

======================


public class FollowCam2 : MonoBehaviour
{
    public GameObject player;
    public Vector3 offset = new Vector3(0f, 1.75f, 0.3f);

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {

        transform.position = player.transform.position + offset;
    }
}


Repeated the exercise with dogs for practice:


5 views0 comments

Recent Posts

See All