Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
DMD2542-Project4-AndrewSides/Final Platformer/Assets/Scripts/PowerUp.cs
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
60 lines (48 sloc)
1.86 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.UI; | |
public class PowerUp : MonoBehaviour | |
{ | |
public Text poweruptext; | |
public Transform gunEnd; | |
public GameObject shot; | |
public float fireRate; | |
private float nextFire; | |
private Rigidbody2D rb; | |
private int powerup; | |
// Start is called before the first frame update | |
void Start() | |
{ | |
rb = GetComponent<Rigidbody2D>(); //gets the rigidbody 2d component | |
powerup = 0; //sets poweup to 0 | |
Setpoweruptext(); | |
} | |
void OnTriggerEnter2D(Collider2D other) | |
{ | |
if (other.gameObject.CompareTag("Rocket") && powerup < 10) //checks if the tag of the object is rocket and checks if powerup is less than 10 | |
{ | |
other.gameObject.SetActive(false); //makes object disappear when player collides with object | |
powerup = powerup + 5; | |
Setpoweruptext(); //sets the object to false on collision and gives the player a powerup (rocket) and displays it on the text | |
GetComponent<AudioSource>().Play(); | |
} | |
} | |
void Setpoweruptext() | |
{ | |
poweruptext.text = "ROCKETS: " + powerup.ToString(); //displays text on the screen to whatever it tells us to display | |
} | |
void Update() | |
{ | |
if (Input.GetButtonDown("Fire2") && Time.time > nextFire) //when the right button is clicked, a rocket is fired, and it subtracts the number of rocket the player has on the text | |
{ | |
if (powerup >= 1) //checks if there is at least 1 rocket, otherwise command won't work | |
{ | |
nextFire = Time.time + fireRate; //sets a fire rate | |
Instantiate(shot, gunEnd.position, gunEnd.rotation); | |
powerup = powerup - 1; | |
poweruptext.text = "ROCKETS: " + powerup.ToString(); | |
} | |
} | |
} | |
} |