Unity C# 脚本不播放粒子效果
我使用 Unity Hub 中的 FPS Microgame 创建了一个项目来学习脚本编写。我试图在玩家与它碰撞时播放一个简单的粒子效果(将来我希望能够从更远的地方触发它,也许在进入飞机时,但我稍后会了解到)。我创建了以下脚本并将其作为组件附加到场景上的粒子效果:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class portalTrigger : MonoBehaviour
{
private void OnTriggerEnter(Collider other)
{
ParticleSystem ps = GetComponent<ParticleSystem>();
ps.Play();
}
}
这是粒子效果在 Unity 检查器中的样子:
如果我选择“唤醒时播放”,则游戏开始时会播放粒子效果,因此我禁用了它,因为我希望它在用户与其碰撞时播放。有什么想法吗?
谢谢!
I created a project using the FPS Microgame in Unity Hub to learn scripting. I'm trying to play a simple particle effect when the player collides with it (in the future I'd love to be able to trigger it from farther away, maybe when crossing into a plane, but I'll learn that later). I created the following script and attached it as a component to the particle effect on the scene:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class portalTrigger : MonoBehaviour
{
private void OnTriggerEnter(Collider other)
{
ParticleSystem ps = GetComponent<ParticleSystem>();
ps.Play();
}
}
And here's what the particle effect looks like in the Unity Inspector:
The particle effect plays when the game starts if I select Play on Wake so I disabled it because I want it to play when the user collides with it. Any ideas?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,我会添加调试打印,以验证碰撞是在ontriggerenter方法中触发的。
如果不是这种情况,我会检查两个游戏对象是否具有刚性的身体和撞机器。两个山口组中的一个(也是一个)必须具有ISTrigger标志集。
这是unity doc链接以供参考:
https://docs.unity3d.com/scriptreferiptreferiptreference/collider/collider/collider.collider.ontrigger.ontrigger.html.html
First I would add a debug print to verify that a collision is triggered in the onTriggerEnter method.
If that is not the case, I would check that both game objects have a rigid body and a collider. One (and only one) of the two colliders must have the isTrigger flag set.
Here is unity doc link for reference:
https://docs.unity3d.com/ScriptReference/Collider.OnTriggerEnter.html
您是否确定对象主对象中的 isTrigger 选项已被检查为 true?
如果没有,那么要么看看检查它的方法(我的意思是,如果你只检查 isTrigger 而不进行另一次碰撞,那么即使在碰撞之后,对象也不会停止。当我开始时,这对我来说真的很忙。)
或使用 OnCollisionEnter。如果这些都没有发生,则尝试打印以查看它们是否发生碰撞。来吧,您正在努力成为一名 Unity 脚本编写者,所以最好的方法是调试您怀疑的任何内容。
Had you quite made sure that the isTrigger option in object main object has been checked true?
If not then either see the way to check it( What do I mean here is that if you only check isTrigger without making another collision than object won't stop even after the collision. It had been really hectic for me when I started.)
or use the OnCollisionEnter. If none of these happened then try printing to see if they're even colliding or not. C'mon you are trying to become a Unity scripter so best way is to debug anything you doubt about.