- Home /
Spawning a projectile with a trigger
Im trying to make a projectile spawn from a turret when an "enemy" tagged object enters its collider or trigger zone. I have 2 empty game objects positioned where I would like the projectiles to spawn from and I also have the projectile ready to go as a prefab. I've been trying to come up with a script myself but I can't seem to get it to work so far. Any help with this code would be greatly appreciated! Steve
I still haven't made any progress. If anyone could help, that would be fantastic.
You are more likely get help if you post your script attempt.
Answer by supernat · Feb 02, 2014 at 06:26 AM
Steve,
It would go something like this. Create 3 public GameObject variables in your script that you will assign values to in the editor. Assign the spawn point A to one variable, spawn point B to one variable, then assign the prefab to the third variable.
Within your script, add an OnCollisionEnter() or OnCollisionEnter2D() method, and create the missile at whichever spawn point you want using the Instantiate method, passing the starting point as the spawn point. You can pass Quaternion.identity for no rotation.
For further reading: http://docs.unity3d.com/Documentation/Manual/InstantiatingPrefabs.html
Your answer
Follow this Question
Related Questions
Spawn a prefab 1 Answer
Play sound on trigger, sound is coming from the trigger 1 Answer
Spawn object problem 1 Answer
Pressing "Fire2" Spawns and removes object on click 0 Answers
Triggers not working - What's wrong with this script? 2 Answers