- Home /
How to emit particle from a collision?
Hi I have some code(the code below) and it works great, but I need it to explode where ever the sphere ends up. The game is I have a hammer. And a sphere as the enemy so whenever the player hits the sphere with his/her hammer it falls(because of the rigidbody) but the particle system emits where the sphere started out. I want it to emit where ever the sphere ends up. Because the sphere doesn't explode till 3 or so seconds.
var explosionPrefab : Transform;
function OnCollisionEnter(collision : Collision)
{
if (collision.gameObject.tag == "Hammer")
{
audio.Play();
// Wait for the audio to have finished
yield WaitForSeconds (audio.clip.length);
Instantiate(explosionPrefab);
Destroy(gameObject);
}
}
@script RequireComponent(AudioSource)
You have 3 questions now with ".... from a collision", what is it you don't understand.Please provide feedback to those who have answered you, or at least accept their answers for their effort.
The answer you are looking for is on this question of yours : http://answers.unity3d.com/questions/407838/how-to-create-particle-system-on-a-collision.html
Answer by LukaKotar · Feb 28, 2013 at 01:14 AM
You should google it or check the reference for Object.Instantiate.
Instantiate() uses three parameters. First is the object you want to instantiate, next is position and rotation. Separate them by commas.
Like that:
Instantiate(explosionPrefab, transform.position, Quaternion.identity);
Answer by elhispano · Dec 06, 2013 at 04:15 PM
I recommend you to use a single ParticleSystem for the same types of bullets and use ParticleSystem.Play() or Emit() (depending on the scenario) each time there is a collision. Using Instantiate and Destroy is going to generate a lot of work for the garbage collector so you can get performance problems.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
How to Destroy a gameobject on collision 3 Answers
destroy child object 2 Answers
problem with shooting killing script 1 Answer
How to delay lines of code. 2 Answers