Advertisement
johnnygoodguy2000

Powerups.cs

Apr 15th, 2024 (edited)
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.50 KB | Gaming | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Powerups : MonoBehaviour
  6. {
  7.     public bool doublePoints;
  8.     public bool safeMode;
  9.    
  10.     public float powerupLength;
  11.  
  12.     public Sprite[] powerupSprites;
  13.  
  14.     private PowerupManager thePowerupManager;
  15.  
  16.     // Start is called before the first frame update
  17.     void Start()
  18.     {
  19.         thePowerupManager = FindObjectOfType<PowerupManager>();
  20.     }
  21.  
  22.     void Awake ()
  23.     {
  24.         int powerupSelector = Random.Range(0, 2);
  25.  
  26.         switch (powerupSelector)
  27.         {
  28.             case 0: doublePoints = true; break;
  29.             case 1: safeMode = true; break;
  30.         }
  31.  
  32.        
  33.  
  34. //need the sprite renderer to be change the sprite based on the random power up. green (powerup1)= double points, blue (powerup2) = safe mode
  35.  
  36. GetComponent<SpriteRenderer>().sprite = powerupSprites[powerupSelector];
  37.  
  38.  
  39.     }
  40.  
  41.  
  42.  
  43.  
  44.  
  45.     // OnTriggerEnter2D is called when the Collider2D other enters the trigger
  46.     void OnTriggerEnter2D(Collider2D other)
  47.     {
  48.         // Check if the colliding object is the player
  49.         if (other.name == "Player")// (other.CompareTag("Player")) commented out would have Player walk through the powerup gem
  50.         {
  51.             // Activate the powerup in the PowerupManager
  52.             thePowerupManager.ActivatePowerup(doublePoints, safeMode, powerupLength);
  53.  
  54.             // Deactivate the powerup object
  55.             gameObject.SetActive(false);
  56.         }
  57.     }
  58. }
  59.  
  60.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement