简体   繁体   中英

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);
    }
}

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 ();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM