- Home /
instantiating vertically
Im making a platformer and made 14 prefabs, each is made of game objects with a collider and trigger attached to them, my player jumps up on platforms, i want to instantiate those 14 prefabs vertically, one after another prefab. I thought of attaching a trigger to the camera that checks if there are any platforms in the area the trigger will cover, if not instantiate. anyone can help? give me any ideas??
Answer by Digital-Phantom · Apr 01, 2015 at 07:02 AM
Put a trigger on each platform so each time your player jumps onto a platform the next on is instantiated
Better yet put your 14 prefabs into an array and then pick on randomly. That way no two play-throughs of your game would be the same
:)
Or make them instantiate by using a yield WaitForSeconds statement so a new prefab appears every few seconds.
Or even just keep track of the players position on the Y axis and instantiate a new prefab as the player moves upwards.
;)
Or even just keep track of the players position on the Y axis and instantiate a new prefab as the player moves upwards. That's exactly what I'm looking for, could you help me on this one tho? how would the code look? I'm frustrated and confused >.<
Still fairly new to this myself but something along the lines of-
if(transorm.position.y >=10)
{
Instantiate(yourPreFab, transform.position.y+10, transform.rotation);
}
Then do additional if >= for 20, 30 and so on...
There is probably a way to do that programmatically in a single if statement, but like I said I'm still quite new to this myself.
arg, could be problematic since each prefab is different size tho. I dont want there to be gaps between prefabs or it wont be playable, and also dont want them to generate one on another if the distance is too short... I have no ideas tho :/
Answer by SamiSiddiqui · Apr 02, 2015 at 06:07 PM
I guess you want some kind of pooling at work. Instantiate Object is player is in range? There are alot of ways to do that. I suggest Instantiate all Objects at start and display when they are required. For the display you can create a JSON file or something to store x,y of platforms in your level. Then check player y and load platforms which are near to player you can take a constant value to subtract and add to player y to get the list of platforms needs to created. But if you want it to precise you can get on start Camera World Top and Camera World Bottom (http://docs.unity3d.com/ScriptReference/Camera.ViewportToWorldPoint.html). Then Add/Subtract that from player y.