Skip to content
Permalink
master
Switch branches/tags

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?
Go to file
 
 
Cannot retrieve contributors at this time
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();
}
}
}
}