- Home /
Question by
Type9Studios · Mar 06, 2012 at 08:47 AM ·
gameobjectnewbie
GameObject Spawning
Hi,
I'm new to Unity (as you can tell), could someone explain (give me a script) how to create an object spawner. Thanks, Liam
Comment
Answer by JohnThEchidna · Feb 08, 2016 at 05:12 PM
Hello, use this code :
public var enemy : Rigidbody;
private var tick : float = 0;
public var speed : float = 5;
public var paso : float = 1;
function Update () {
tick+= Time.deltaTime;
speed = Random.Range(0.0f, 5.0f);
offsetZ = Random.Range(-30.0f, 30.0f);
//offsetY = Random.Range(-3.0f, 3.0f);
offsetX = Random.Range(-30.0f, 30.0f);
offsetY = 1.0f;
if (tick>=paso){
tick = 0;
var position : Vector3 = new Vector3(transform.position.x+offsetX,transform.position.y, transform.position.z+offsetZ);
var instantiatedProjectile : Rigidbody = Instantiate(enemy, position, transform.rotation);
instantiatedProjectile.velocity = transform.TransformDirection(Vector3(0,-speed,0));
}
}