【问题标题】:Can someone tell me how to make a cooldown for the bullet?有人能告诉我如何为子弹做冷却吗?
【发布时间】:2022-07-21 18:31:36
【问题描述】:
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);
        }
    }
}

有谁知道如何让子弹有冷却时间,请告诉我? 一个人说要对输入做点什么,所以当子弹射击时它有冷却时间。 `

【问题讨论】:

标签: c# unity3d


【解决方案1】:

这是一个使用计时器的简单方法,应该类似于

 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.
 }
 
 
 }

希望它有效!

【讨论】:

  • targetTime -= Time.deltaTime - 最好只测量经过的时间。无论如何,前者都会受到浮点误差的影响。
猜你喜欢
  • 1970-01-01
  • 2021-12-08
  • 1970-01-01
  • 1970-01-01
  • 2023-01-14
  • 1970-01-01
  • 2021-05-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多