- Home /
Spread Gameobjects on Terrain
is it possible to write a code that spreads a number (for example 5) of a gameobject (for example the enemy) within a specific room on the terrain when pressing play? the gameobjects should be spread by coincidence and therefore on different places everytime i press play. thank you!!
Answer by wolga2 · Jul 04, 2012 at 06:10 PM
the code works perfectly, thank you so much!! my problem now is, that the instantiated object is a navmeshagent and it will be placed on non-navmesh-places as well. is there a solution to avoid this? thanks!
Answer by wolga2 · Jul 04, 2012 at 03:25 PM
the code works perfectly, thank you so much!! but my problem is now, that the object is a navmeshagent and it will put the instantiated object on non-navmesh-places as well. what can i do about that? thanks!
Answer by khellstr · Jul 04, 2012 at 11:13 AM
http://docs.unity3d.com/Documentation/ScriptReference/Object.Instantiate.html
Should get started with this:
var prefab : Transform;
for (var i : int = 0;i < 5; i++) {
Instantiate (prefab, Vector3(Random.value, Random.value, Random.value), Quaternion.identity);
}
Of course, you have to set certain borders for the random value (not that the enemy spawns somewhere outside the room, or floats in the air above it) ;-)
Random.value returns something between 0.0 - 1.0, so probably it have to be multiplied, depending of scene size. Also should check if the place is free where game object spawn. and other stuff...
I'm pretty sure there is ready-made spawner script some where in internets. Recommend google.
google, yes, but the question is here and the asker is most likely a new user. Here's a more concise answer showing the points in the above comments :
#pragma strict
public var instPrefab : Transform;
public var instAmount : int = 5;
public var instRange : Vector3 = Vector3( 5, 0, 2 ); // range of random position, represents -value to +value
function Start()
{
SpawnObjects();
}
function SpawnObjects()
{
var instPosition : Vector3;
for (var i : int = 0; i < instAmount; i++) // loop through until the amount is reached
{
instPosition = Vector3( Random.Range(-instRange.x, instRange.x), Random.Range(-instRange.y, instRange.y), Random.Range(-instRange.z, instRange.z) ); // create a random position, from -instRange to +instRange
Instantiate (instPrefab, instPosition, Quaternion.identity); // Instantiate the object there
}
}
Sorry about that google, did not mean to be patronizing.
For me it's often so that I know what I want to do but don't know the right words for searches. English is not my native language. So I though "spawn" could be key word for OP, he doesn't not mention it in question but is clearly what he needs to do. So probably I should have written try google "spawn script" etc.
Your answer
![](https://koobas.hobune.stream/wayback/20220613073704im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
how would i add bullet spread? 0 Answers
Spread material over multiple objects or cubes 1 Answer
Maya pistol animation splits up when played 0 Answers
Gas/Liquid spreading in area 1 Answer
access DOF values via script 1 Answer