Having Player light torches Using Particle System and OnCollisionEnter?
Hello,
I'm trying to get my player to light torches in a scene, but not quite sure how to do this.
I have a torch prefab, that has a particle system and each time the player's torch hits an unlit torch, I would like that torch to start.
My current code is below. I have each torch object tagged as torch, and my player tagged as Player.
Any advice or tips?
Thanks!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/* Attach this script to all torches. It will be used to start the fire using OnCollision?/OnTrigger?
* Start with the particle effect/light being off, get all the components
* Turn the torches on when the player's torch collides with them
* Must make sure each torch object has a collider
**/
public class StartFire : MonoBehaviour
{
public GameObject torch;
public ParticleSystem fireParticleSystem;
bool lightOn;
void Start()
{
//Start with the light off
lightOn = false;
//get Particle System
fireParticleSystem = GetComponent<ParticleSystem>();
//get Torch
torch = GetComponent<GameObject>();
}
// if player's torch hits this torch (that is not lit)
//Turn on the fire
//Set the light being on to true
private void OnCollisionEnter(Collision collision)
{
if (this.gameObject.tag==("torch") && collision.gameObject.tag==("Player") && lightOn==false)
{
//start the particle system
fireParticleSystem.Play();
lightOn = true;
}
}
}
Answer by dmayers340 · Apr 10, 2018 at 08:47 AM
I had also asked this question on StackOverflow (https://stackoverflow.com/questions/49733313/unity3d-having-player-light-torches-using-particlesystem-and-oncollisionenter) and this response (by someone with the handle Programmer) worked for me so wanted to share:
"1.Create your ParticleSystem and change the tag of its GameObject to "torch".
2.Attach BoxCollider to the SphereCollider to that GameObject with the ParticleSystem.
3.Mark the IsTrigger of the collider created from #2 to be true because it doen't make sense to collide with a touch. It seems like you just want to detect when the player is touching it.
4.The touch script should attached to the player instead of the touch. Use OnTriggerEnter to handle the detection and detect when player touches the touch-light then use GetComponent to get the ParticleSystem and play it. Stop the particle in OnTriggerExit.
If you actually want player to collide and be stopped by the touch then ignore #2 and also use OnCollisionEnter and OnCollisionExit instead of OnTriggerEnter and OnTriggerExit.
Attach to the Player:
public class ParticlePlayer : MonoBehaviour
{
void OnTriggerEnter(Collider other)
{
//Make sure player is touching a touch
if (other.CompareTag("torch"))
{
//Get ParticleSystem from the Gameobject the player collided with
ParticleSystem ps = other.GetComponent<ParticleSystem>();
//Play Particle
ps.Play();
}
}
void OnTriggerExit(Collider other)
{
//Make sure player is touching a touch
if (other.CompareTag("torch"))
{
//Get ParticleSystem from the Gameobject the player collided with
ParticleSystem ps = other.GetComponent<ParticleSystem>();
//Stop Particle
ps.Stop();
}
}
}
"
So huge shout out to that person who answered. I hope this can help someone else as well!
Your answer
Follow this Question
Related Questions
C# scripting and Particle System incompatibility 1 Answer
Moving particle system via script causes choppy movement in build but works fine in pay mode 1 Answer
changing the radius of a particle system with unity script 1 Answer
Moving a singular particle to the cursor 0 Answers
Is it possible to create a touch input on spawned particles? 0 Answers