Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unity3d 需要在粒子系统停止播放后立即禁用它_Unity3d - Fatal编程技术网

Unity3d 需要在粒子系统停止播放后立即禁用它

Unity3d 需要在粒子系统停止播放后立即禁用它,unity3d,Unity3d,我正在尝试安装火焰喷射器。我已经为flame设置了粒子系统。它是池对象。它不是火焰喷射器的孩子。当我按下“点火”按钮时,粒子系统启动,当按钮打开时,粒子系统停止。但是一个问题出现了,当玩家移动时,粒子系统不会移动。通过添加以下行解决了此问题 particles.transform.position = transform.GetChild(0).position; 但我发现了另一个问题,当旋转播放器(这是一个2D侧滚游戏)时,粒子会立即随它旋转。因此,当玩家改变方向时,当前粒子停止,新粒子被激

我正在尝试安装火焰喷射器。我已经为flame设置了粒子系统。它是池对象。它不是火焰喷射器的孩子。当我按下“点火”按钮时,粒子系统启动,当按钮打开时,粒子系统停止。但是一个问题出现了,当玩家移动时,粒子系统不会移动。通过添加以下行解决了此问题

particles.transform.position = transform.GetChild(0).position;
但我发现了另一个问题,当旋转播放器(这是一个2D侧滚游戏)时,粒子会立即随它旋转。因此,当玩家改变方向时,当前粒子停止,新粒子被激活并播放。但现在的问题是,每当我在按下点火按钮的同时改变方向时,就会创建新的对象

我的火焰喷射器的代码是 使用UnityEngine

public class FlameThrower : Gun
{
    private ParticleSystem particles;

    private int direction = 0;
    private bool isFiring = false;

    public override void Update()
    {
        if(shoot)
        {
            InitShoot();
        }
        else
        {
            StopFire();
        }
    }

    public override void InitShoot()
    {
        if(!isFiring)
        {
            SelectDirection();
            Fire();
        }

        //Check direction has changed
        if(direction != playerManager.direction)
        {
            StopFire();
        }

        if(particles != null)
        {
            particles.transform.position = transform.GetChild(0).position;
        }
    }

    public override void Fire()
    {
        isFiring = true;
        direction = playerManager.direction;

        InstantiateParticles(weapon.bulletName, transform.GetChild(0).position, rotation);
    }

    public override void SelectDirection()
    {
        if (playerManager.direction == 1)
        {
            rotation = Quaternion.Euler(transform.rotation.x, transform.rotation.y, -90);
        }
        else if (playerManager.direction == -1)
        {
            rotation = Quaternion.Euler(transform.rotation.x, transform.rotation.y, 90);
        }
    }

    public override void InstantiateParticles(string name, Vector3 position, Quaternion rotation)
    {
        GameObject bullet = ObjectPooler.instance.GetObject(name);

        while (bullet == null)
        {
            ObjectPooler.instance.CreateObject(name);
            bullet = ObjectPooler.instance.GetObject(name);
        }

        if (bullet != null)
        {
            particles = bullet.GetComponent<ParticleSystem>();
            //Set Position and rotation
            bullet.transform.position = position;
            bullet.transform.rotation = rotation;
            bullet.SetActive(true);
            particles.Play();
        }
    }

    private void StopFire()
    {
        if (particles != null)
        {
            isFiring = false;
            particles.Stop();
            if(!particles.isPlaying)
            {
                particles.gameObject.SetActive(false);
                particles = null;
            }
        }
    }
}
公共级火焰喷射器:枪
{
私有粒子系统粒子;
私有int方向=0;
private bool isFiring=假;
公共覆盖无效更新()
{
如果(拍摄)
{
InitShoot();
}
其他的
{
停止射击();
}
}
public override void initshot()
{
如果(!正在点火)
{
选择方向();
火();
}
//检查方向已更改
if(方向!=playerManager.direction)
{
停止射击();
}
如果(粒子!=null)
{
particles.transform.position=transform.GetChild(0).position;
}
}
公共火灾
{
isFiring=true;
方向=playerManager.direction;
实例化部件(武器.bulletName,transform.GetChild(0).位置,旋转);
}
公共覆盖无效SelectDirection()
{
if(playerManager.direction==1)
{
旋转=四元数.Euler(transform.rotation.x,transform.rotation.y,-90);
}
else if(playerManager.direction==-1)
{
旋转=四元数.Euler(transform.rotation.x,transform.rotation.y,90);
}
}
公共覆盖无效实例化粒子(字符串名称、矢量3位置、四元数旋转)
{
GameObject bullet=ObjectPooler.instance.GetObject(名称);
while(bullet==null)
{
ObjectPooler.instance.CreateObject(名称);
bullet=ObjectPooler.instance.GetObject(名称);
}
如果(项目符号!=null)
{
粒子=bullet.GetComponent();
//设置位置和旋转
bullet.transform.position=位置;
bullet.transform.rotation=旋转;
bullet.SetActive(true);
粒子。Play();
}
}
私人停车场
{
如果(粒子!=null)
{
isFiring=false;
粒子。停止();
如果(!particles.isPlaying)
{
particles.gameObject.SetActive(false);
粒子=零;
}
}
}
}

问题在于,在函数StopFire()中,它检查粒子是否正在播放。如果它不在玩,它将禁用游戏对象。但这部分不会执行,因为在粒子停止后不久会对其进行检查,并且仍将播放。我希望此粒子系统在停止播放后立即禁用

粒子系统似乎正在局部空间中渲染,这就是它随对象旋转的原因

模拟空间
更改为
世界

对于另一个问题,如果禁用游戏对象,则已发射的粒子将消失。如果您通过延迟(使用Invoke或类似方法)禁用它们,则在该延迟内可以发射新粒子

处理此问题的最佳方法是停止发射,而不是停用游戏对象。您可以在inspector中或通过代码执行此操作<代码>myParticles.emission.enabled


粒子系统似乎在局部空间中渲染,这就是它随对象旋转的原因

模拟空间
更改为
世界

对于另一个问题,如果禁用游戏对象,则已发射的粒子将消失。如果您通过延迟(使用Invoke或类似方法)禁用它们,则在该延迟内可以发射新粒子

处理此问题的最佳方法是停止发射,而不是停用游戏对象。您可以在inspector中或通过代码执行此操作<代码>myParticles.emission.enabled

使用UnityEngine;
公共类ParticleSystemControllerWindow:单行为
{
粒子系统
{
得到
{
如果(_CachedSystem==null)
_CachedSystem=GetComponent();
返回缓存系统;
}
}
私有粒子系统(CachedSystem);;
public Rect windowRect=新Rect(0,0,300,120);
公共bool includeChildren=true;
void OnGUI()
{
windowRect=GUI.Window(“ParticleController.GetHashCode(),windowRect,DrawWindowContents,system.name);
}
无效DrawWindowContents(int windowId)
{
if(系统)
{
GUILayout.BeginHorizontal();
GUILayout.Toggle(system.isplay,“Playing”);
GUILayout.Toggle(system.i发射,“发射”);
GUILayout.Toggle(system.isPaused,“暂停”);
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
if(GUILayout.按钮(“播放”))
系统。游戏(包括儿童);
if(GUILayout.按钮(“暂停”))
系统暂停(包括儿童);
如果(GUILayout.按钮(“停止发射”))
系统停止(包括儿童、粒子系统停止行为、停止发射);
if(GUILayout.按钮(“停止并清除”))
系统停止(包括儿童、粒子系统停止行为、停止发射和清除);
GUILayout.EndHorizontal();
includeChildren=GUILayout.Toggle(includ
using UnityEngine;

public class ParticleSystemControllerWindow : MonoBehaviour
{
ParticleSystem system
{
    get
    {
        if (_CachedSystem == null)
            _CachedSystem = GetComponent<ParticleSystem>();
        return _CachedSystem;
    }
}
private ParticleSystem _CachedSystem;

public Rect windowRect = new Rect(0, 0, 300, 120);

public bool includeChildren = true;

void OnGUI()
{
    windowRect = GUI.Window("ParticleController".GetHashCode(), windowRect, DrawWindowContents, system.name);
}

void DrawWindowContents(int windowId)
{
    if (system)
    {
        GUILayout.BeginHorizontal();
        GUILayout.Toggle(system.isPlaying, "Playing");
        GUILayout.Toggle(system.isEmitting, "Emitting");
        GUILayout.Toggle(system.isPaused, "Paused");
        GUILayout.EndHorizontal();

        GUILayout.BeginHorizontal();
        if (GUILayout.Button("Play"))
            system.Play(includeChildren);
        if (GUILayout.Button("Pause"))
            system.Pause(includeChildren);
        if (GUILayout.Button("Stop Emitting"))
            system.Stop(includeChildren, ParticleSystemStopBehavior.StopEmitting);
        if (GUILayout.Button("Stop & Clear"))
            system.Stop(includeChildren,  ParticleSystemStopBehavior.StopEmittingAndClear);
        GUILayout.EndHorizontal();

        includeChildren = GUILayout.Toggle(includeChildren, "Include Children");

        GUILayout.BeginHorizontal();
        GUILayout.Label("Time(" + system.time + ")");
        GUILayout.Label("Particle Count(" + system.particleCount + ")");
        GUILayout.EndHorizontal();
    }
    else
        GUILayout.Label("No particle system found");

    GUI.DragWindow();
}
}