先上方法

//如果Fire1按钮被按下(默认为ctrl),每0.5秒实例化一发子弹

public GameObject projectile;
    public float fireRate = 0.5F;
    private float nextFire = 0.0F;
    void Update() {
        if (Input.GetButton("Fire1") && Time.time > nextFire) {
            nextFire = Time.time + fireRate;
            duck clone = Instantiate(projectile, transform.position, transform.rotation);
        }
    }

 还有一个简答的方法就是使用MonoBehaviour.InvokeRepeating 重复调用:

在2S后,每隔0.5s进行子弹的实例化。

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
public GameObject projectile;
    public void Awake() {
        InvokeRepeating("Weapon", 2, 0.5F);
    }
    
    void Weapon() {
            duck clone = Instantiate(projectile, transform.position, transform.rotation);
    }
    
}

 

相关文章:

  • 2021-08-10
  • 2021-07-30
  • 2022-12-23
  • 2021-10-23
  • 2021-10-11
  • 2022-01-21
  • 2022-12-23
  • 2021-12-06
猜你喜欢
  • 2021-05-08
  • 2021-12-24
  • 2022-01-27
  • 2022-01-25
  • 2022-12-23
  • 2022-12-23
  • 2021-09-18
相关资源
相似解决方案