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!
CodePudding user response:
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
CodePudding user response:
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.