Skip to content

Instantly share code, notes, and snippets.

@TheMehranKhan
Created October 27, 2023 20:51
Show Gist options
  • Select an option

  • Save TheMehranKhan/15e1f2345de124c87ac56c7e6a9162af to your computer and use it in GitHub Desktop.

Select an option

Save TheMehranKhan/15e1f2345de124c87ac56c7e6a9162af to your computer and use it in GitHub Desktop.

Revisions

  1. TheMehranKhan created this gist Oct 27, 2023.
    57 changes: 57 additions & 0 deletions PowerUp.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,57 @@
    /*
    Author: themehrankhan
    License: MIT License
    Description:
    This code block provides a power-up system functionality in Unity. It allows the player to collect power-ups and apply their effects.
    Usage:
    1. Attach this script to the power-up object in Unity.
    2. Set the power-up effect and duration in the inspector.
    */

    using UnityEngine;

    public class PowerUp : MonoBehaviour
    {
    public PowerUpEffect effect; // Power-up effect
    public float duration = 5f; // Duration of the power-up effect

    private void OnTriggerEnter(Collider other)
    {
    if (other.CompareTag("Player"))
    {
    ApplyEffect(other.gameObject);
    Destroy(gameObject);
    }
    }

    private void ApplyEffect(GameObject player)
    {
    switch (effect)
    {
    case PowerUpEffect.SpeedBoost:
    player.GetComponent<PlayerMovement>().speed *= 2f;
    Invoke(nameof(ResetEffect), duration);
    break;
    case PowerUpEffect.Invincibility:
    player.GetComponent<HealthSystem>().enabled = false;
    Invoke(nameof(ResetEffect), duration);
    break;
    // Add more power-up effects here
    }
    }

    private void ResetEffect()
    {
    // Reset power-up effect here
    }
    }

    public enum PowerUpEffect
    {
    SpeedBoost,
    Invincibility
    // Add more power-up effects here
    }