Search code examples
c#animationunity-game-enginecollisionparticle-system

Emit Particle onCollision in Unity 3D


I have a character that is going to collide with a coin. When the character collides with the coin, a particle "animation" should occur. Here's my code so far. Some basic assistance would help a lot. This code is attached to the player character.

    void OnTriggerEnter(Collider _hit)
{
    if (_hit.tag == "Coin")
    {
        Destroy(_hit.gameObject);
        coinCount++;
        coinsText.text = "Coins: " + coinCount.ToString() + "/" + coinTotal.ToString();
        var Bling : GameObject = Instantiate(Bling, transform.position, Quaternion.identity);
    }
}

Solution

  • This is what you need to do.

    public ParticleSystem collisionParticlePrefab; //Assign the Particle from the Editor (You can do this from code too)
    private ParticleSystem tempCollisionParticle;
    
    void OnTriggerEnter (Collider _hit)
    {
        if (_hit.tag == "Coin") {
            Destroy (_hit.gameObject);
            coinCount++;
            coinsText.text = "Coins: " + coinCount.ToString() + "/" + coinTotal.ToString();
            tempCollisionParticle = Instantiate (collisionParticlePrefab, transform.position, Quaternion.identity) as ParticleSystem;
            tempCollisionParticle.Play ();
        }
    }