- Home /
Question by
Jammer3000 · Mar 09, 2014 at 03:10 PM ·
javascriptinstantiaterandom
How to instantiate 2D object at fixed location?
Hey jeremy here! What I'm trying to do is instantiate an object at a random position within the radius of a sphere or cube! which I have done and works fine, the thing I need is for it to instantiate a random amount of them in between 1 and 10 and then wait about 2 seconds and then do it again?
var projectile : Rigidbody2D;
InvokeRepeating("LaunchProjectile", 1, 0.2);
function LaunchProjectile () {
var instance : Rigidbody2D = Instantiate(projectile);
instance.velocity = Random.insideUnitSphere * 10;
}
Comment
Best Answer
Answer by Jammer3000 · Mar 09, 2014 at 06:01 PM
The code below creates 10 objects(projectiles) and spawns them from the location of the projectile rigidbody2D in the scene and then spreads them out in the area of a sphere with a radius of 10.
var projectile : Rigidbody2D;
function Start () {
for (var i : int = 0; i < 10; i++) {
var instance : Rigidbody2D = Instantiate(projectile);
instance.velocity = Random.insideUnitSphere * 10;
Debug.Log("for loop");
}
}