x


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!

more ▼

asked Nov 13 '10 at 03:10 AM

pepperedereppep gravatar image

pepperedereppep
101 26 27 35

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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.

more ▼

answered Nov 13 '10 at 04:21 AM

Eric5h5 gravatar image

Eric5h5
80.3k 42 132 521

That codes made it so much easier, and now everything works :) Awesome reply. Thanks for your time.

Nov 13 '10 at 05:19 AM pepperedereppep
(comments are locked)
10|3000 characters needed characters left

You already have the Speed variable; what's wrong with just doing

speed += Time.deltaTime * 0.1;

in Update?

more ▼

answered Nov 13 '10 at 03:59 AM

Loius gravatar image

Loius
10.6k 1 11 41

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1673
x1278
x764
x206

asked: Nov 13 '10 at 03:10 AM

Seen: 1438 times

Last Updated: Nov 13 '10 at 03:10 AM