- Home /
instantiate,destroy,gain speed in time
Not sure if the topic makes any sense, but basically I'm trying to instantiate a sphere(prefab) every 1 second, destroy the sphere after 2-5 seconds, and all the while have the spheres translating in x axis and speeding up faster and faster
var balls:Transform; var bool:boolean=true; var speed:float=0;
function Update () { if(bool==true){ ballsIns(); } //balls.transform.position.x+=speed*Time.deltaTime; }
function ballsIns(){ bool=false; var balls=Instantiate(balls,Vector3.zero,Quaternion.identity); yield WaitForSeconds(1); bool=true; }
this code is not exactly working obviously. The balls instantiate, but they don't translate. The sphere prefab has a code to destroy itself after a few seconds. My main question here is how to write a code that would speed up the balls in time. I can't write the code within the sphere prefab itself because once it destroys itself it resets the value. Any help would be great. Thanks!
Answer by Eric5h5 · Nov 13, 2010 at 04:21 AM
To spawn the balls, you can have this script on some object in the scene, like the camera or an empty game object:
var instantiateInterval = 1.0; var destroyTimeMin = 2.0; var destroyTimeMax = 5.0; var ball : GameObject;
function Start () { InvokeRepeating("Spawn", .01, instantiateInterval); }
function Spawn () { var ballClone = Instantiate(ball); Destroy(ballClone, Random.Range(destroyTimeMin, destroyTimeMax)); }
You can have a script on the ball prefab which translates it on the x axis. For the increasing speed, there are a couple of possibilities, depending on the effect you want:
If you want each ball to have its own speed, you can have a speed variable on the manager script, which you assign to a speed variable on the ball's script, using GetComponent when you instantiate the ball prefab. You would increment the speed variable after instantiating each ball.
Or you could have a static variable in the manager script, which likewise is incremented after each instantiation, and the ball's movement script would multiply its speed by the static speed variable in the manager script. That way all balls would always be going the same speed.
That codes made it so much easier, and now everything works :) Awesome reply. Thanks for your time.
Answer by Loius · Nov 13, 2010 at 03:59 AM
You already have the Speed variable; what's wrong with just doing
speed += Time.deltaTime * 0.1;
in Update?
Your answer
Follow this Question
Related Questions
How do I duplicate an object to work with a shootemup 0 Answers
Bullet Translation question 0 Answers
Can't remove instantiated prefab 0 Answers
Destroying & Then Spawning Quickly 1 Answer
Destroying assets is not permitted to avoid data loss. 6 Answers