- Home /
How to simply enable an objects particle system emission when that object collides with another?
Basically, the player runs into the coin and the coin emits particles. I've looked up how to do this but all solutions through enabled emission are depreciated. I am lost, here is what I have so far
And here is all the code I have (don't laugh I'm a beginner)
Answer by ignacevau · Dec 25, 2018 at 05:40 PM
You can simply play the Particle System by using CoinParticleSystem.Play
I'm not sure whether you had this already but for triggering after the collision, you can use OnCollisionEnter
(make sure that both objects have colliders). In code it would look something like this:
public ParticleSystem CoinParticleSystem;
private void OnCollisionEnter(Collision collision)
{
CoinParticleSystem.Play();
}
Yes, I have a player script that increments the score with the coin. like so
void OnTriggerEnter2D(Collider2D other) { if (other.tag == "Star") myGameController.Incrementscore(); myAudioPlayer.PlayOneShot(scoreSFX); Destroy(other.gameObject); }
It's on the trigger though so should I go into the particle system editor and make it one trigger?
If I understood you correctly, every coin has a particle system component and when the player hits the coin, the OnTriggerEnter2D
method is called inside the Player's script? In that case, you simply have to write:
private void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Star")
{
other.GetComponent<ParticleSystem>().Play();
myGameController.Incrementscore();
myAudioPlayer.PlayOneShot(scoreSFX);
Destroy(other.gameObject);
}
}
Yes thats exactly right! But can I refrence the particle system from any script or do I have to attach the playerscript to the coin and then put the particle system in through the inspector.
As well as make a particle system variable to refrence in the playerscript? Public particlesystem coinparticlesystem;
Your answer
Follow this Question
Related Questions
Is it possible to set one particle system equal to another through script? 0 Answers
Using AirBrakes on mobile device with Standard Assets AircraftController 0 Answers
UniRPG Error cs0121. 0 Answers
Learn: Survival Shooter aim offset from mouse position 1 Answer
Shoot particles like motorcycle roost... 0 Answers