Skip to main content

On Sale: GamesAssetsToolsTabletopComics
Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines
(+1)

I noticed a few game didn't have a pause menu so I feel like this might be useful some later down the line. Even if this is outside of your normal engine I hope that my code can inspire other's solutions that may even help debug a problem they may have. Feel free to modify this code as you see fit, I can always rewrite or refactor any of this as needed!

Menu Transition Code: This is the code I used in Unity3D to make the menu seamlessly switch between each other. 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Menutransition : MonoBehaviour
{
    public GameObject _StartingScreen;
    public GameObject _TargetScreen;
    public Animator _ScreenAnimator;
    public float _TimeDelay;
    public void Transitioning()
    {
        StartCoroutine(SwitchingScreens());
    }
    IEnumerator SwitchingScreens()
    {
        _ScreenAnimator.Play("Swipe");
        yield return new WaitForSecondsRealtime(_TimeDelay);
        _TargetScreen.SetActive(true);
        _StartingScreen.SetActive(false);
    }
}

Floating Object

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Floating_object : MonoBehaviour
{
    Vector3 startPos;
    // the height that the object will float above and back down from
    public float amplitude = 10f;
    // the time that the object will take to float to the height
    public float period = 5f;
    protected void Start()
    {
        startPos = transform.position;
    }
    protected void Update()
    {
        float theta = Time.timeSinceLevelLoad / period;
        float distance = amplitude * Mathf.Sin(theta);
        transform.position = startPos + Vector3.up * distance;
    }
}

Heal or Damage: I mixed these two together so that I wouldn't need to make a bunch of scripts while still keeping things optimized.  Simply by toggling the bool, this can either damage or heal the player. It's pretty useful especially if you have a game object that can heal and damage.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HealorDamage : MonoBehaviour
{
    public bool _Healer;
    public GameObject _Target;
    public float _HealthPoints;
    public SphereCollider _SelfCollider;
    public float _Cooldown;
    private void Start()
    {
        _SelfCollider = this.gameObject.GetComponent<SphereCollider>();
    }
    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.CompareTag("Player") || (other.gameObject.CompareTag("Enemy")))
        {
            _Target = other.gameObject;
            if (_Healer == true)
            {
                Healing();
            }
            else
            {
                Damaging();
            }
        }
    }
    void Healing()
    {
        if (_Target.GetComponent<CharacterHealth>()._HealthCurrent < 50)
        {
            _Target.GetComponent<CharacterHealth>().GainHealth(_HealthPoints);
            print("Have some HP!");
        }
        else
        {
            print("You're already at max health!");
            StartCoroutine(ColliderToggle());
            return;
        }
    }
    void Damaging()
    {
        if (_Target.GetComponent<CharacterHealth>()._HealthCurrent > 0)
        {
            _Target.GetComponent<CharacterHealth>().TakeDamage(_HealthPoints);
            print("Give me that HP!");
        }
        else
        {
            print("You're suppose to be dead!");
            StartCoroutine(ColliderToggle());
            return;
        }
    }
    IEnumerator ColliderToggle()
    {
        _SelfCollider.enabled = false;
        print("collider off");
        yield return new WaitForSeconds(_Cooldown);
        _SelfCollider.enabled = true;
        print("collider on");
    }
}

Character Health: This one is similar to the HealorDamage in how it can be used on both a player and an enemy as long as a bool is checked off. The logic for the health lives and energy do the same thing but they do require images to display the info. 

using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using TMPro;
using UnityEngine;
public class CharacterHealth : MonoBehaviour
{
    [Header("Health")]
    public float _HealthMax;
    public float _HealthCurrent;
    public Image _HealthBarSource;
    [Header("Energy")]
    public float _EnergyMax;
    public float _EnergyCurrent;
    public Image _EnergyRingSource;
    [Header("Lives")]
    public int _LivesMax;
    public int _LivesCurrent;
    public TMP_Text _LivesSource;
    public GameObject _Explosion;
    public bool _IsEnemy;
    public void Start()
    {
        _HealthCurrent = _HealthMax;
        _EnergyCurrent = _EnergyMax;
        _LivesCurrent = _LivesMax;
        if (_IsEnemy == false)
        {
            LinkUI();
        }
        else
        {
            ClearUI();
        }
    }
    public void Update()
    {
        Death();
        if (_IsEnemy == false)
        {
            _HealthBarSource.fillAmount = (_HealthCurrent * 0.02f);
            _EnergyRingSource.fillAmount = (_EnergyCurrent * 0.04f);
            
        }
    }
    private void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag("Death"))
        {
            gameObject.transform.position = new Vector3(0, 0, 0);
            Instantiate(_Explosion, gameObject.transform.position, gameObject.transform.rotation);
        }
    }
    void LinkUI()
    {
        _HealthBarSource.GetComponent<Image>();
        _EnergyRingSource.GetComponent<Image>();
    }
    void ClearUI()
    {
        _HealthBarSource = null;
        _EnergyRingSource = null;
        _LivesSource = null;
    }
    public void GainHealth(float _Hp)
    {
        _HealthCurrent += _Hp;
        print("Player has been healed");
    }
    public void TakeDamage(float _Hp)
    {
        _HealthCurrent -= _Hp;
        print("Player has been harmed");
    }
    void Death()
    {
        if (_HealthCurrent <= 0 && _IsEnemy)
        {
            Instantiate(_Explosion, gameObject.transform.position, gameObject.transform.rotation);
            Destroy(gameObject);
            print(gameObject + " has died");
        }
        if (_HealthCurrent <= 0 && !_IsEnemy)
        {
            print("Restart the damn game!");
        }
    }
}