[英]Can someone tell me how to make a cooldown for the bullet?
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);
}
}
}
有誰知道如何讓子彈有冷卻時間,請告訴我? 一個人說要對輸入做點什么,所以當子彈射擊時它有冷卻時間。 `
這是一個使用計時器的簡單方法,它應該類似於
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.
}
}
希望它有效!
在另一個答案中,您只能拍攝一次,然后您必須等待。 我會以一種可以多次拍攝然后過熱的方式創建它。 您可能需要修改此代碼並使用數字以使其按您的意願工作,但這就是想法。
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
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.