KDamon
October 10, 2017, 10:29pm
1
I’m making an overhead shooter game, and I made it so when the player touches a certain object, their fire rate increases. How can I make it so the power up wears off after a few seconds?
public class PlayerPickups : MonoBehaviour {
PlayerHealth playerHealth;
public float RFBoostValue=0.005f;
GameObject player;
void Awake()
{
player = GameObject.FindGameObjectWithTag("Player");
playerHealth = player.GetComponent<PlayerHealth>();
}
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Rapid Fire Pickup")
{
Destroy(other.gameObject);
PlayerShooting.timeBetweenBullets -= RFBoostValue;
}
}
}
Use a coroutine- add the effect to the character at the beginning, wait X seconds, then remove the effect before ending. You can find more information on coroutines here .
KDamon
October 10, 2017, 10:51pm
3
Heres’ what I tried, but the power up’s not wearing off.
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Rapid Fire Pickup")
{
Destroy(other.gameObject);
PlayerShooting.timeBetweenBullets -= RFBoostValue;
PowerUpWearOff();
}
}
IEnumerator PowerUpWearOff()
{
yield return new WaitForSeconds(4);
PlayerShooting.timeBetweenBullets += RFBoostValue;
}
johne5
October 10, 2017, 10:57pm
4
like this
public class PlayerPickups : MonoBehaviour {
PlayerHealth playerHealth;
public float RFBoostValue=0.005f;
GameObject player;
void Awake()
{
player = GameObject.FindGameObjectWithTag("Player");
playerHealth = player.GetComponent<PlayerHealth>();
}
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Rapid Fire Pickup")
{
Destroy(other.gameObject);
PowerUpWearOff(5f); //start the time function
}
}
IEnumerator PowerUpWearOff(float waitTime)
{
PlayerShooting.timeBetweenBullets -= RFBoostValue; // add boost
yield return new WaitForSeconds(waitTime);
PlayerShooting.timeBetweenBullets += RFBoostValue; // remove boost
}
}
KDamon
October 10, 2017, 11:13pm
5
I’m trying that code out, but the fire rate’s not changing. It’s like it was increased just to be decreased again.
That should be StartCoroutine(PowerUpWearOff(5f));
If you call it as a normal function, the delay won’t work.
Note that the way this is set up, multiple ones will stack, and so you could reduce your shot time to negative if you picked up enough at once.
KDamon
October 10, 2017, 11:51pm
7
Errorsatz:
That should be StartCoroutine(PowerUpWearOff(5f));
If you call it as a normal function, the delay won’t work.
Note that the way this is set up, multiple ones will stack, and so you could reduce your shot time to negative if you picked up enough at once.
is there anyway to set a limit? So I can’t collect two at once?
johne5
October 11, 2017, 12:24am
8
here you go
public class PlayerPickups : MonoBehaviour {
PlayerHealth playerHealth;
public float RFBoostValue=0.005f;
GameObject player;
private bool isActive = false;
void Awake()
{
player = GameObject.FindGameObjectWithTag("Player");
playerHealth = player.GetComponent<PlayerHealth>();
}
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Rapid Fire Pickup" && isActive == false)
{
isActive = true;
Destroy(other.gameObject);
StartCoroutine(PowerUpWearOff(5f)); //start the time function
}
}
IEnumerator PowerUpWearOff(float waitTime)
{
PlayerShooting.timeBetweenBullets -= RFBoostValue; // add boost
yield return new WaitForSeconds(waitTime);
PlayerShooting.timeBetweenBullets += RFBoostValue; // remove boost
isActive = false;
}
}