1
\$\begingroup\$

I've made a game where you can shoot some cans, but I want to add an effect when a can is shot. You know, a kind of explosion or something like that. The way the game works now is: when the can is shot it disappears destroy(gameobject), but I want to add an effect before it disappears. Any suggestions? I want to keep it simple. The effect has to be in within the same method where I destroy the cans.

   public void Shot()
{
    //Effect should be here
    Destroy(gameObject);
}
\$\endgroup\$
  • \$\begingroup\$ There are countless ways to do that. One you might want to look into is a particle effect. \$\endgroup\$ – Philipp Oct 24 '16 at 17:11
  • \$\begingroup\$ Create an actual 3D animation that plays at the collision location. Treat it as an entity. \$\endgroup\$ – Krythic Oct 25 '16 at 13:37
3
\$\begingroup\$

For a 3D game, a simple particle effect spawned at the location of the can would be easy enough to do. For a 2D game, you could use a particle effect or a sprite animation at the point of impact. Heck, add a sound effect. Doing any of those could happen inside your Shot() function, ideally by calling a Subroutine. Just don't make the effect a child of the can, or it won't finish before the object is destroyed.

\$\endgroup\$
  • \$\begingroup\$ Can you please show me how it's done in the method Shot()? I've tried to do it but can't make it work \$\endgroup\$ – user4476151 Oct 24 '16 at 17:23
  • \$\begingroup\$ Since you're using Unity, I would create a particle effect that you like, create a prefab from it by dragging it from the project hierarchy to the Prefabs folder, then create a public ParticleSystem variable in the script, drag and drop the prefab into that slot in the inspector, then just Instantiate from that variable anytime hit is called. \$\endgroup\$ – Jesse Williams Oct 24 '16 at 17:30
  • 1
    \$\begingroup\$ No prefabs are gameObjects, it must be public gameObject \$\endgroup\$ – UDXS Oct 24 '16 at 17:33
  • \$\begingroup\$ Ah - this is true. I guess I minced my thoughts there. You could create an empty object, attach a particle system to it, put it in prefabs and do what I said... or, you can just build a particle system you like and drag and drop it from whatever location into the script if you use a ParticleSystem var. \$\endgroup\$ – Jesse Williams Oct 24 '16 at 17:35
  • \$\begingroup\$ +1 for this is essentially the more abstract way to do what I said to do. \$\endgroup\$ – UDXS Oct 24 '16 at 17:38
0
\$\begingroup\$

I recommend you create a prefab with a particle system(or set of them) that also has a despawn timer script attached to it. The particles can be used for an explosion. The despawn script will wait a certain amount of time or until particle systems finish and will be attached to the top-level of the prefab. If you don't know about Prefabs, I suggest you research them because they are uber useful. You can use the Instantiate(Object theObject) function to spawn that prefab. You can make a public GameObject field in the MonoBehaviour of that Shot() method. This would allow you to simply drag the prefab from the Asset Explorer into the properties window for that object.

I hope this helped, if unclear, ask me to fix it.

\$\endgroup\$
  • \$\begingroup\$ To be honest, I do understand it, but don't know how to do it. I do know what prefab is. How do I activate the particle system when can is hit? \$\endgroup\$ – user4476151 Oct 24 '16 at 17:26
  • \$\begingroup\$ instanciate the prefab, if the particle system is configured correctly, it should run once. \$\endgroup\$ – UDXS Oct 24 '16 at 17:28
  • \$\begingroup\$ Have done as you describe but now, when I shoot the cans I get this error: UnassignedReferenceException: The variable effect of Shotclass has not been assigned. You probably need to assign the effect variable of the Hit script in the inspector \$\endgroup\$ – user4476151 Oct 24 '16 at 20:32
  • \$\begingroup\$ In addition to that, the particle system is also running \$\endgroup\$ – user4476151 Oct 24 '16 at 20:33
0
\$\begingroup\$

Create a particle on top of your script

public GameObject effect;

then in your function

public void Shot()
{
    //Effect should be here
    Instanciate(effect, transform.position, transform.rotation);
    Destroy(gameObject);
}

Then in the editor, create an empty gameobject containing you effect and drag it on your script.

\$\endgroup\$
  • \$\begingroup\$ Have done as you describe but now, when I shoot the cans I get this error: UnassignedReferenceException: The variable effect of Shotclass has not been assigned. You probably need to assign the effect variable of the Hit script in the inspector \$\endgroup\$ – user4476151 Oct 24 '16 at 20:31
  • \$\begingroup\$ In addition to that, the particle system is also running \$\endgroup\$ – user4476151 Oct 24 '16 at 20:33
  • \$\begingroup\$ Have fixed it, but the particle system is running eve before I shoot a can \$\endgroup\$ – user4476151 Oct 24 '16 at 20:48
  • \$\begingroup\$ Is the effect a child of the can ? If so it should not. \$\endgroup\$ – Ezkin Oct 25 '16 at 9:49
0
\$\begingroup\$

You should have a look at this SpaceShooter Tutorial from the Unity Website. Especially the Explosions Section https://unity3d.com/de/learn/tutorials/projects/space-shooter-tutorial/explosions?playlist=17147

You might want to tweak this particle Effect to your liking

Now all you need to do is to spawn this particle Effect before your bullet gets destroyed.

Beware to make sure that you destroy your Particle Effect after it has stopped.

You could do this like so:

using UnityEngine;
using System.Collections;

public class DestroyAfterStopped : MonoBehaviour {

    private ParticleSystem myParticleSystem;

    // Use this for initialization
    void Start () {
        myParticleSystem = GetComponent<ParticleSystem>();
    }

    // Update is called once per frame
    void Update () {
        if(myParticleSystem.isStopped)
        {
            if (gameObject != null)
            {
                Destroy(gameObject);
            }
        }
    }
}

Just assign this Script to your ParticleSystem.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.