Funny Story

Okay, so, funny story, I already implemented a game over for when the player died when I hooked up all the stuff from the previous lecture. I also made a delay to loading the game over on the player script because the player’s death sound is a bit longer.
This is the script, if anyone’s curious:

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

public class Player : MonoBehaviour
{
    //config params
    [Header("Player")]
    [SerializeField] float moveSpeed = 10f;
    [SerializeField] float padding = 1f;
    [SerializeField] int health = 200;

    [Header("Player laser")]
    [SerializeField] GameObject laserPrefab;
    [SerializeField] float projectileSpeed = 10f;
    [SerializeField] float firePeriod = 0.1f;
    [SerializeField] AudioClip playerShot;
    [Range(0f, 5f)] [SerializeField] float shotVolume = 1.0f;

    [Header("Death")]
    bool dead = false;
    [SerializeField] GameObject explosionVFX;
    [SerializeField] AudioClip playerExplosion;
    [Range(0f,5f)] [SerializeField] float explosionVolume = 1.0f;

    //coroutine
    Coroutine autoFire;


    float xMin;
    float xMax;
    float yMin;
    float yMax;

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

    

    // Update is called once per frame
    void Update()
    {
        if (!dead)
        {
            Move();
            Fire();
        }
    }

    private void Fire()
    {
        if (Input.GetButtonDown("Fire1"))
        {
            autoFire = StartCoroutine(Autofire());
        }
        if (Input.GetButtonUp("Fire1"))
        {
            StopCoroutine(autoFire);
        }
    }

    IEnumerator Autofire()
    {
         while (!dead)
         {
             LaserFire();
             yield return new WaitForSeconds(firePeriod);
         }
    }

    private void LaserFire()
    {
        GetComponent<AudioSource>().PlayOneShot(playerShot, shotVolume);
        GameObject laser = Instantiate(laserPrefab, transform.position, Quaternion.identity) as GameObject;
        laser.GetComponent<Rigidbody2D>().velocity = new Vector2(0, projectileSpeed);
    }

    private void Move()
    {
        var deltaX = Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed;
        var newXPos = Mathf.Clamp(transform.position.x + deltaX, xMin, xMax);
        var deltaY = Input.GetAxis("Vertical") * Time.deltaTime * moveSpeed;
        var newYPos = Mathf.Clamp(transform.position.y + deltaY, yMin, yMax);
        transform.position = new Vector2(newXPos, newYPos);
    }
    private void OnTriggerEnter2D(Collider2D other)
    {
        DamageDealer damageDealer = other.gameObject.GetComponent<DamageDealer>();
        if (!damageDealer) { return; }
        ProcessDamage(damageDealer);
        damageDealer.Hit();
    }
    private void ProcessDamage(DamageDealer damageDealer)
    {
        health -= damageDealer.GetDamage();
        if (health == 0)
        {
            PlayerDeath();
        }
    }

    private void PlayerDeath()
    {
        dead = true;
        Instantiate(explosionVFX, transform.position, transform.rotation);
        AudioSource.PlayClipAtPoint(playerExplosion, Camera.main.transform.position, explosionVolume);
        StartCoroutine(GameOver());
    }

    IEnumerator GameOver()
    {
        yield return new WaitForSeconds(4.75f);
        FindObjectOfType<SceneLoader>().LoadGameOver();
    }


    private void SetUpMoveBounderies()
    {
        Camera gameCamera = Camera.main;
        xMin = gameCamera.ViewportToWorldPoint(new Vector3(0, 0, 0)).x + padding;
        xMax = gameCamera.ViewportToWorldPoint(new Vector3(1, 0, 0)).x - padding;
        yMin = gameCamera.ViewportToWorldPoint(new Vector3(0, 0, 0)).y + padding;
        yMax = gameCamera.ViewportToWorldPoint(new Vector3(0, 1, 0)).y - padding;
    }
}

Is it right for me to be a little proud of myself for that?

3 Likes

Ah, found an error with my logic ^^’

Privacy & Terms