x


Instantiate prefabs just before it comes into view

Im making a platform type game and I was trying to figure out how to Instantiate a prefab as the the player(object) about every 5 units in the y direction.

Ive got the actual Instantiate working

//////////////////////////////////////////// var prefab : Transform; var playerCam : Transform; var player : Transform;

function Update () {

var playerCamY = playerCam.position.y;
var abovePlayer = playerCamY+7;
var belowPlayer = playerCamY-7;

var randPlaceX = Random.Range(-2,2);
var randPlaceY = Random.Range(abovePlayer,abovePlayer+5);


Instantiate (prefab, Vector3(randPlaceX,randPlaceY,-0.1738763),Quaternion.identity); 

}

////////////////////////////////////////////////

But I just need a way of telling WHEN to Instantiate. Is there something like InvokeRepeating but for my own variable rather than seconds.

I Ultimately want to get the prefabs to Instantiate before it enters the frame then destroy it soon after it leaves the frame.

Any help would be greatly appreciated, Ive been scratching my head for ages on this one....

more ▼

asked Jan 09 '12 at 12:21 PM

nickazg gravatar image

nickazg
31 3 3 3

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

2 answers: sort voted first

ahh, i also just realized how slow Instantiate's are and causes a lag every Instantiate. So instead im either going to have it all Instantiate before gameplay or have only like 10 clones and reposition them just before they come into view

more ▼

answered Jan 10 '12 at 03:53 AM

nickazg gravatar image

nickazg
31 3 3 3

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

Well, if you know where the camera is and where it is pointing and where it is going, you could key it off camera position!

So, for example-

var instantiationGap : float = 3;
var nextInstantiation : float 0;

function Update()
{
    var currentPos = cameraTransform.position.y;
    if(currentPos >= nextInstantiation)
    {
        // Spawn something!
        nextInstantiation += instantiationGap;
    }
}

This way, it will spawn an object when the camera reaches certain milestones.

more ▼

answered Jan 09 '12 at 12:36 PM

syclamoth gravatar image

syclamoth
15k 7 15 80

WOO! thanks man that worked. I cant believe i didnt come up with that. so simple. cheers. Now i just need to do the opposite and destroy it when it leaves the cam.

Jan 10 '12 at 12:47 AM nickazg
(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:

x3565
x1725
x1284
x784

asked: Jan 09 '12 at 12:21 PM

Seen: 904 times

Last Updated: Jan 10 '12 at 03:53 AM