- Home /
how do I instantiate random gameobject every certrain meters the player moves?
First of all, thank you for trying to help me! Like in subway runner, etc. the order of the gameobjects are really random, and every certain distance there's a new random GameObject (PowerUp). let me clarify, my question is that I create endless runner right now, for exp. there's a blade "level" (with many rotating blades), a lazer "level", monster "level", etc. I call them "levels" but I want that after a certain amount of distance the player moves, (for exp. 500 meters) a new RANDOM "level" will instantiate. for exp. someone start the game and the first level he does is the blade level, but if someone else also starts the game, there's a chance it would be the monster level, and after a certain amount of meters he passes, the last level is "destroyed" and the new RANDOM level (except the level he just did) is instantiated. I hope you catched what I'm trying to say, and could help me :) I'd appreciate really much any help at all! Thanks a lot!
keep track of the distance the player runs. whenever he passes like 5 meters, subtract them from the distance and instantiate a blade 5 meters infront of the last one you spawned. to prespawn a few just fake the distance the player ran and subtract and instantiate as long as there's 5 meters left.
Answer by tormentoarmagedoom · Oct 24, 2018 at 11:50 AM
Good day.
Its simple, make a vector3 variable to store the last position of the player when a new level was instantiates (commencing in 0)
Then you only need to check in the update the distance between that point ant the actual position of player.
Void Update()
{
if( Vector3.distance (lastPosition, Player.transform.position) >= 500)
{
NewLevelFunction();
lastPosition = Plyer.transform.position;
}
}
Thats all!
Bye!
Your answer
Follow this Question
Related Questions
random between 3 gameobjects 1 Answer
random limitation memory game 0 Answers
Is this the best way of using enums? 0 Answers
Lightning with random intervals 2 Answers
Best way to instantiate lots of objects 0 Answers