- Home /
Triggering particles with code
I am creating a simple platformer. When the player collides with the ground he respawns at the begening of the level. I also want a short little particle system to trigger and shoot out from him. I decided to write that part in the same script as the respawning. I can't get it to work though. I hope someone can help. (The respawning works fine, just need help with particle system) (C#)
using UnityEngine;
using System.Collections;
public class KillZone : MonoBehaviour {
GameObject player;
Vector3 respawn;
var particle ParticleSystem;
void Start () {
player = GameObject.Find ("Player");
respawn = GameObject.Find ("Respawn").transform.position;
}
void OnTriggerEnter2D (Collider2D col) {
Debug.Log ("Killzone Collision Detected");
particle.Play ();
player.transform.position = respawn;
}
}
This looks weird
var particle ParticleSystem;
Don't you mean?
ParticleSystem particle;
Also, you don't seem to be assigning the particle
variable anywhere.
That works great, thanks for the help. Sorry for my lack of knowledge!
I converted my comment with the script to an answer. Please mark it as accepted to help others having a similar problem.
Answer by Cherno · Jul 07, 2016 at 12:55 PM
using UnityEngine;
using System.Collections;
public class KillZone : MonoBehaviour {
GameObject player;
Vector3 respawn;
public ParticleSystem particle ;
void Start () {
player = GameObject.Find ("Player");
respawn = GameObject.Find ("Respawn").transform.position;
}
void OnTriggerEnter2D (Collider2D col) {
Debug.Log ("Killzone Collision Detected");
particle.Emit(30);
player.transform.position = respawn;
}
}
Of course, you somehow have to assign a value to the particle variable, either by script or by dragging your ParticleSystem into the field next to the variable in the inspector.