Home > Enterprise >  Can someone tell me how to make a cooldown for the bullet?
Can someone tell me how to make a cooldown for the bullet?

Time:07-22

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BulletParticle : MonoBehaviour
{
    public float damage = 10f;

    public ParticleSystem particleSystem;

    public GameObject spark;

    List<ParticleCollisionEvent> colEvents = new List<ParticleCollisionEvent>();

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Mouse0))
        {
            particleSystem.Play();
        }
    }

    private void OnParticleCollision(GameObject other)
    {
        int events = particleSystem.GetCollisionEvents(other, colEvents);

        for (int i = 0; i < events; i  )
        {
            Instantiate(spark, colEvents[i].intersection, Quaternion.LookRotation(colEvents[i].normal));
        }

        if (other.TryGetComponent(out enemy en))
        {
            en.TakeDamage(damage);
        }
    }
}

Does anyone know how to make the bullet have a cooldown please tell me? A guy said to do something with the input so when the bullet shoots it has a cooldown. `

CodePudding user response:

that's a simple method using a timer it should be something like

 using UnityEngine;
 using System.Collections;
 
 public class SimpleTimer: MonoBehaviour {
 

// i assigned the timer at 3 seconds
 public float cooldownBullet = 3.0f;

 
 public bool canShoot=false;
 
 Update(){
 
 targetTime -= Time.deltaTime;

 if (Input.GetKeyDown(KeyCode.Mouse0) && canShoot==true)
 {
            canShoot=false;
            particleSystem.Play();
 }
 
 if (targetTime <= 0.0f)
 {
    canShoot=true;
 }
 
 }
 
 void timerEnded()
 {
    //do your stuff here.
 }
 
 
 }

Hope it works!

CodePudding user response:

In the other answer, you can shoot only once, and then you have to wait. I would create it in a way that you can shoot multiple times and then it overheats. You might need to modify this code and play with the numbers to make it work as you want but that is the idea.

using UnityEngine;
     using System.Collections;
     
     public class CoolDownTimer: MonoBehaviour {
     
     public float heat=0.0f;
     public bool canShoot=true;
     public float heatLimit=10.0f;
    
     Update(){
     
        if(heat > 0f)
          heat -= Time.deltaTime;
    
        if(heat < heatLimit)
           canShoot = true;
    
        if (Input.GetKeyDown(KeyCode.Mouse0) && canShoot==true)
        {
                particleSystem.Play();
                heat  ;
                if(heat >= heatLimit){
                   canShoot=false
                 }
        }    
    }      
}
  • Related