- Home /
Programming help with instantiate
Well the idea is every second to spawn the original game object 5 meters in front of the previous one. Example say 0 is the mesh: 0--0--0--0--0 and by the fifth zero five seconds have gone on and on the 6th second it will look like this: 0--0--0--0--0--0. Now I know I should use instantiate to spawn a new mesh (without destroying the original), and combine it with Time.deltaTime.
Here's what I have come up with:
function Update () {
//I don't know how to format the instantiate , so here is what I think it would be like:
gameObject = instantiate
var translation = Time.deltaTime * 5;
transform.Translate (0, 0, translation);
}
I really have very little experience in programming, although I do know how it works.
Why the downvote there were no other questions that answered my question.
Gotta agree with @jack, I don't see any major problems with this question. So +1. I will make one suggestion, though - bookmark this page: http://unity3d.com/support/documentation/$$anonymous$$anual/index.html as well as the Scripting tab on right. If you don't know how a function works (like Instantiate) go to the Scripting page and search for it. That will at least show you the arguments, and some usage examples.
Answer by cncguy · Jul 09, 2010 at 11:30 PM
Hi try the following
var preFab : GameObject; //Set this in the inspector.
var spawnPnt : Vector3 = Vector3(0,0,0); // set the initial spawn point var incrementVec : Vector3 = Vector3(0,0,5.0); // set the offset per spawn
function Start() { InvokeRepeating("Spawn",0.5, 1.0); //Set up a call to Spawn() every second }
function Spawn() { Instantiate(preFab, spawnPnt, Quaternion.identity); //Instantiate your object at spawnPnt spawnPnt += incrementVec; //Set the next spawnPnt }
Answer by fherbst · Jul 09, 2010 at 07:33 PM
Use something like this.
// this script should NOT be added to the object you want to instantiate, // but to any other one (empty gameObject, for example).
// the time that shall pass before a new instance is created public var timeStep = 5.0; // the object that shall be instantiated public var myObject : Transform; // the move offset between objects public var moveOffset : Vector3 = (0,0,2);
// the last time a new instance was created private var lastTime : float; private var number = 0;
function Start() { // start at zero time difference lastTime = Time.time; }
function Update() { // compare current time to last time a object was instantiated if(Time.time - lastTime > timeStep) { // keep track of the number of objects number++; // instantiate your object at a offset position with no rotation Instantiate(myObject, moveOffset * number, Quaternion.identity); } }
Time.deltaTime
returns the seconds since the last Update()
was called, and is commonly used to make movements and other things frame rate independent. For your case, Time.time
suits better, because it returns the total amount of time since you started your game.
I hope it works, although I haven't tested it.
I assume myObject is intended to be the object it instantiates? I seems much more complicated than I would have though, but will test it later.
Oh, I think it only looks complicated - it isn't really more than your code if you look at the Update() function, everything else are just initializations.