版本使用Unity 2022.3.40fcl,文章内容可能随版本变化发生错误,请以最新版引擎内容为准,如有出现改变,可留言询问。
本文使用Unity引擎,发文时团结引擎与Unity引擎操作一致。
我在制作游戏的过程中,经常会遇到让物体发射一个模型,比如发射子弹,在触发一次发射指令时会出现一次性发射多个模型的情况,影响游戏效果,现在分享一个解决方法以供参考。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
protected Transform m_transform;
//指向子弹的Transform
public Transform m_rocket;
// Start is called before the first frame update
//Use this for initialization
void Start()
{
invokeTime = currentTime;
}
// Update is called once per frame
void Update()
{
{
//按空格键或鼠标左键发射子弹
if ( Input.GetKey( KeyCode.Space ) || Input.GetMouseButton(0) )
{
//按键按下时进行计时
invokeTime += Time.deltaTime;
//间隔时间大于自定义时间才执行
if ( invokeTime - currentTime > 0)
{
Instantiate( m_rocket, m_transform.position, m_transform.rotation );
//实例化一次后计时归零
invokeTime = 0;
}
}
}
}
}
2万+

被折叠的 条评论
为什么被折叠?



