- Home /
Prefab to spawn on right spot
I wanted my prefab to spawn over and over again and used this --> http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.InvokeRepeating.html
It's works almost perfect - The Prefab spawns over and over again but now on the right spot...
I created a new "GameObject" and added the script but the prefab spawns all over the map - Anyone know how I can fix this?
Answer by Kourosh · Apr 18, 2011 at 08:33 PM
You need to define a spawn point and pass the position of it to the Instantiate function. To do so simply create an empty gameobject and rename it as "SpawnPoint" and then place it where you want your prefabs be instantiated from. Then in your script do this:
var spawnPoint:Transform; //assign "SpawnPoint".
function LaunchProjectile () { var instance : Rigidbody = Instantiate(projectile, spawnPoint.position, Quaternion.identity);
//To Shoot in random directions: instance.velocity = Random.insideUnitSphere * 5; //This is causing it to shoot all over the place. cause it's spawning in random directions. whereas if you want a defined direction you would use next line instead.
//To have a predefined direction use this line: instance.velocity = spawnPoint.forward * 5; //make sure the forward axis of the spawn point gameobject is looking at your desired direction.
}
Thx for the post, I created a new gameobject and renamed it but I just get a $$anonymous$$ identifier: 'Projectile'. .....
Ye, cause you need to declare the projectile variable which I didn't bother doing. It's already in the link you used in your question.
Your answer
![](https://koobas.hobune.stream/wayback/20220613123625im_/https://answers.unity.com/themes/thub/images/avi.jpg)