YueZHeng_917 2022-07-08 14:45 采纳率: 61.1%
浏览 326
已结题

Unity攻击间隔的代码怎么写?

问题遇到的现象和发生背景
问题相关代码,请勿粘贴截图
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class shell1n : MonoBehaviour
{
    public GameObject ShellPrefab;//public子弹预制体
    public KeyCode FireKey = KeyCode.Space;//FireKey设置为空格键
    public float shellSpeed = 10;//子弹速度为10
    private Transform firePoint1;//发射位置FirePoint1
    public GameObject sphereshellExplosionPrefab;//public子弹爆炸粒子效果预制体
    public bool Fire1 = true;//子弹发射许可设置为true
    public float jiangetime = 3.0f;//攻击间隔jiangetime设置为3.0秒

    void Start()
    {
        firePoint1 = transform.Find("FirePoint1");//自动寻找发射位置FirePoint1        
    }
    void Update()
    {
        if (Fire1 == true)//如果子弹发射许可为ture,执行下方内容
        {
            if (Input.GetKeyDown(FireKey))//如果按下了FireKey,执行下方内容
            {
                Fire1 = false;//攻击后进入间隔,子弹发射许可设置为false
                GameObject go = GameObject.Instantiate(ShellPrefab, firePoint1.position, firePoint1.rotation) as GameObject;//在发射点位置实例游戏对象ShellPrefab。
                go.GetComponent<Rigidbody>().velocity = go.transform.forward * shellSpeed;//给予子弹初速度
//(这里这里!!!!!!我是第28行!!!!!!!!!!)//攻击间隔,等待jiangetime,时间达3.0秒
                Fire1 = true;//攻击间隔冷却结束,子弹发射许可设置为ture
            }
        }
    }
    public void OnTriggerEnter(Collider collider)
    {
        GameObject.Instantiate(sphereshellExplosionPrefab, transform.position, transform.rotation);//实例化爆炸效果
        GameObject.Destroy(this.gameObject);//删除子弹
    }
}


运行结果及报错内容
我的解答思路和尝试过的方法
我想要达到的结果

我要在第28行代码填写什么,才能在Unity中达成按下FireKey后,三秒钟内按下FireKey按键无反应?
求简短一点,如果没有这样的代码,能不能给可怜的题主指一个这种代码的教程?

  • 写回答

1条回答 默认 最新

  • 周周的Unity小屋 2022-07-08 15:54
    关注

    给你代码做了修改,有用请采纳!

    
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
     
    public class shell1n : MonoBehaviour
    {
        public GameObject ShellPrefab;//public子弹预制体
        public KeyCode FireKey = KeyCode.Space;//FireKey设置为空格键
        public float shellSpeed = 10;//子弹速度为10
        private Transform firePoint1;//发射位置FirePoint1
        public GameObject sphereshellExplosionPrefab;//public子弹爆炸粒子效果预制体
        public bool Fire1 = true;//子弹发射许可设置为true
        public float jiangetime = 3.0f;//攻击间隔jiangetime设置为3.0秒
    
        void Start()
        {
            firePoint1 = transform.Find("FirePoint1");//自动寻找发射位置FirePoint1        
        }
        float time = 0;//时间累加
        void Update()
        {
            if (Input.GetKeyDown(FireKey))//如果按下了FireKey,执行下方内容
            {
                if (Fire1)
                {
                    Fire1 = false;//攻击后进入间隔,子弹发射许可设置为false
                    GameObject go = GameObject.Instantiate(ShellPrefab, firePoint1.position, firePoint1.rotation) as GameObject;//在发射点位置实例游戏对象ShellPrefab。
                    go.GetComponent<Rigidbody>().velocity = go.transform.forward * shellSpeed;//给予子弹初速度     
                }
            }
            else if (!Fire1)
            {
                time += Time.deltaTime;
                if (time>jiangetime)//大于你的时间间隔3秒按下空格键才可以发射子弹
                {
                    Fire1 = true;
                    time = 0;
                }
            }
        }
        public void OnTriggerEnter(Collider collider)
        {
            GameObject.Instantiate(sphereshellExplosionPrefab, transform.position, transform.rotation);//实例化爆炸效果
            GameObject.Destroy(this.gameObject);//删除子弹
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 7月16日
  • 已采纳回答 7月8日
  • 创建了问题 7月8日

悬赏问题

  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事: