gpt4 book ai didi

c# - unity ParticleSystem 播放和停止

转载 作者:太空宇宙 更新时间:2023-11-03 15:13:48 31 4
gpt4 key购买 nike

我的 particlesystem.Play() 不工作。我在这个问题上花了几个小时,但仍然无法弄清楚。

每当我的角色击中一个物体时,它会调用函数 particleAuraPlay(),并且显示日志消息“HIT”,这意味着该函数被正确调用。

playAura 设置为 true 时,粒子应该播放,并且日志消息“Running”也再次出现。由于消息正在显示,我假设我的逻辑是正确的,但粒子不会开始播放。谁能解决我的问题?

using UnityEngine;
using System.Collections;

public class ParticleController : MonoBehaviour {

private bool playAura = false;
private ParticleSystem particleObject;

void Start () {
particleObject = GetComponent<ParticleSystem>();
particleObject.Stop();
}


void Update () {
if (playAura)
{
Debug.Log("Running");
particleObject.Play();
}

}

public void particleAuraPlay()
{
Debug.Log("HIT");
playAura = true;
}
}

最佳答案

When the playAura is set to true, the particle should play, and again the log message "Running" is showing up, too. Since the message is showing I assume my logic is correct but the particle just won't start playing

这是一个逻辑错误。

调用 particleAuraPlay() 函数时,playAura 设置为 true

在更新函数中,particleObject.Play();每帧 调用,因为 playAuratrue.

你不能这样做。调用 particleObject.Play(); 每一帧 实际上不会做任何事情,因为它会尝试在每一帧中播放和停止粒子,从而导致根本没有粒子。

解决方法是检查playAura是否为true,如果为true,调用particleObject.Play(); 函数然后将 playAura 设置为 false 以便 particleObject.Play(); 函数不会被再次调用,直到 particleAuraPlay () 再次被调用。

新的Update()函数修复了逻辑错误:

void Update()
{
if (playAura)
{
Debug.Log("Running");
particleObject.Play();
playAura = false;
}
}

关于c# - unity ParticleSystem 播放和停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39917004/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com