Skip to content

Instantly share code, notes, and snippets.

@TheMehranKhan
Created October 27, 2023 20:51
Show Gist options
  • Save TheMehranKhan/15e1f2345de124c87ac56c7e6a9162af to your computer and use it in GitHub Desktop.
Save TheMehranKhan/15e1f2345de124c87ac56c7e6a9162af to your computer and use it in GitHub Desktop.
This code block provides a power-up system functionality in Unity. It allows the player to collect power-ups and apply their effects.
/*
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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment