C# 当我的timeBetweenShot变量出现时,对象正在被垃圾处理,有人能看一下吗?
我已经创建了一个2d隐身游戏,敌人向玩家开火,唯一的问题是,尽管子弹在另一个脚本上创建和删除得很好,但脚本本身会在每一帧向程序发送子弹,产生不想要的结果C# 当我的timeBetweenShot变量出现时,对象正在被垃圾处理,有人能看一下吗?,c#,unity3d,C#,Unity3d,我已经创建了一个2d隐身游戏,敌人向玩家开火,唯一的问题是,尽管子弹在另一个脚本上创建和删除得很好,但脚本本身会在每一帧向程序发送子弹,产生不想要的结果 using System; using System.Collections; using System.Collections.Generic; using UnityEngine; public class HurtPlayer : MonoBehaviour { public float timeToShoot; private fl
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HurtPlayer : MonoBehaviour
{
public float timeToShoot;
private float timeToShootCounter;
private bool shot;
private Vector3 moveDirection;
public float timeBetweenShot;
public float timeBetweenShotCounter;
public Transform firePoint;
public GameObject Bullet;
// Use this for initialization
void Start()
{
shot = false;
timeToShootCounter = timeToShoot;
}
// Update is called once per frame
void Update()
{
while (shot == true)
{
StartCoroutine(Delay());
Destroy(GameObject.Find("Bullet"));
timeBetweenShot -= Time.deltaTime;
timeToShoot -= Time.deltaTime;
}
}
IEnumerator Delay()
{
yield return new WaitForSeconds(0.5f);
}
void OnTriggerStay2D(Collider2D other)
{
if (other.gameObject.tag == "player")
{
if (shot == false)
{
if (timeToShoot >= 0f)
{
shot = true;
if (shot == true)
{
shot = false;
Instantiate(Bullet, firePoint.position, firePoint.rotation);
Delay();
if (timeBetweenShot <= 0f)
{
shot = false;
timeToShoot = timeToShootCounter;
timeBetweenShot = timeBetweenShotCounter;
}
}
}
}
}
}
使用系统;
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类玩家:单一行为
{
公共浮动时间;
专用浮点时间计数器;
私人枪战;
专用矢量3移动方向;
公共浮动时间间隔shot;
计数器之间的公共浮动时间;
公共转换点;
公共游戏对象子弹;
//用于初始化
void Start()
{
shot=false;
timeToShootCounter=timetoshot;
}
//每帧调用一次更新
无效更新()
{
while(shot==true)
{
start例程(Delay());
销毁(GameObject.Find(“子弹”);
timeBetweenShot-=Time.deltaTime;
timetoshot-=Time.deltaTime;
}
}
IEnumerator延迟()
{
收益率返回新的WaitForSeconds(0.5f);
}
无效OnTiggerStay2D(碰撞的R2D其他)
{
如果(other.gameObject.tag==“玩家”)
{
如果(shot==false)
{
如果(TIMETHOOT>=0f)
{
shot=true;
如果(shot==true)
{
shot=false;
实例化(Bullet、firePoint.position、firePoint.rotation);
延迟();
如果(timeBetweenShot这就是你要找的吗
IEnumerator ContinuousShoot()
{
// Continuously spawn bullets until this coroutine is stopped
// when the player exits the trigger.
while (true)
{
yield return new WaitForSeconds(1f); // Pause for 1 second.
Instantiate(Bullet, firePoint.position, firePoint.rotation);
}
}
void OnTriggerEnter2D(Collider2D other)
{
// Player enters trigger
if (other.gameObject.CompareTag("player"))
{
StartCoroutine(ContinuousShoot());
}
}
void OnTriggerExit2D(Collider2D other)
{
// Player exits trigger
if (other.gameObject.CompareTag("player"))
{
StopCoroutine(ContinuousShoot());
}
}
标题是让您简要解释您的问题,而不是说“我的代码不工作”我认为这个逻辑对于这样的任务来说太复杂了。为什么不在OnTriggerStay2D
中得到一个标志,并且当标志为真时,每0.5秒实例化一个项目符号?此外,更新中的while循环是完全无用的。你还没有完全冻结编辑器的唯一原因是shot
除了inst之外从来都不是真的ant在OnTriggerStay
中被立即再次设置为false。您的延迟
没有任何作用。它不会延迟随后出现的代码,即使您使用Start例程正确调用了它。直接调用它不会将代码作为协同程序运行,但即使它这样做了,您也只能延迟协同程序中的代码。最后,您只需编写if(shot)
if(shot)