- Home /
Creating Infinite Run using Application.LoadLevelAsync
Hello Developers,
I would like to create infinite Run game where new Terrain will continue to generate in front. I want to go with Application.LoadLevelAsync function of Unity. Is this possible to achieve using Application.LoadLevelAsync operation?
Say i have 3 levels:
Level 1
Level 2
Level 3
I want o repeat these levels as player first person player continue to move forward. Please help how can i achieve this using Application.LoadLevelAsync operation if possible. I want to keep first person continue to run.
Thanks and Regards Niraj Vishwakarma
Yes it possible, but you're using the wrong method, you want LoadLevelAdditiveAsync, otherwise with LoadLevelAsync once active, the previous level's junk(with the exception of what was marked as do not destory) will be destroyed. The first thing you can do for yourself and lookup the documentation
Actually there is no clear documentation. As far as i understand the levels get added continuously but not destroyed once the player crossed the level 1. Actually i am developing this for mobile and if levels are adding without destroying the last level i think it will require more CPU overhead. Am i correct here or something else?
I find the documentation clear, but that is just me, if by clear you mean they need more examples that would help your particular situation... meh?
For performance your best bet is to create a system where you don't need to load levels on the fly during play, that the platforms your player is running under are what move and your player doesn't move forward(though the appearance is there). You create a pooling system for your different models, the object you create and code(scripts) should be generic enough to apply to most objects and have properties/fields that allow placement on the field within ranges so you can randomize between.
With a system like that in place you load up everything during the start of the game. If your game is heavy, make natural "resting" points in the game where you can switch out the next set of objects that represent a change in location/level/whatever.
Answer by KMKxJOEY1 · Jul 26, 2014 at 05:13 PM
Instead of loading an entire level, take a look at the Instantiate method where you can spawn a prefab in instead of an entire level
Yes this is True and i am doing the same. Now the problem i am facing that i have to create Trees in front of camera visible area. How can i achieve this, I have Tree prefab and would like to generate in front of visible camera area. I just want to generate Random but unique vector positions in front of camera area say within 200 $$anonymous$$eters?
Answer by viju · Aug 22, 2016 at 12:38 PM
An alternate way of doing it is to create a long 'Plane' 3D object (Scale: X= 50, Y=1, Z=500; Position: 0, Rotation: 0). Let us call it 'Ground'. Create a green material and apply it to the plane to give it a green landscape look.
Create another plane in the middle (Scale: X=1, Y=1, Z=500; Position: X=0, Y=0.2, Z=0, Rotation: 0). Let us call it 'Path'. Create a dark-grey material and apply it to the path.
Move the Main Camera (Position: X=0, Y=10, Z=-1809; Rotation: X=20, Y=0, Z=0).
Place a capsule 3D object on the path (Position: X=0, Y=1.5, Z=-1800) and make the Main Camera as the child of the capsule object.
Add three random objects on the path: Cube (Position: X=-3, Y=1.5, Z=-800), Sphere (Position: X=-4, Y=1.5, Z=-1700) and Cylinder (Position: X=3, Y=1.5, Z=-1300)
Add a RigidBody component to the capsule (Check 'Use Gravity' and under 'Constraints > Freeze Rotation', check X, Y, Z).
Finally, add the following C# code to the capsule object.
using UnityEngine;
using System.Collections;
public class PlayerBehaviour : MonoBehaviour
{
// Update is called once per frame
void FixedUpdate()
{
transform.Translate(Vector3.forward * 0.75f);
if (Input.GetKey(KeyCode.LeftArrow))
transform.Translate(Vector3.left * 0.25f);
if (Input.GetKey(KeyCode.RightArrow))
transform.Translate(Vector3.right * 0.25f);
if (Input.GetKey(KeyCode.UpArrow))
transform.Translate(Vector3.up * 0.25f);
if (transform.position.z > -700f)
{
transform.position = new Vector3(transform.position.x, transform.position.y, -2000f);
}
}
}
We can place a lot of random objects on the 'Ground' and 'Path' and Randomize the environment when transform.position.z > -700f to get a better infinite feel. No need to create new terrain in this case. Just randomize the environment.
Your answer
![](https://koobas.hobune.stream/wayback/20220613155031im_/https://answers.unity.com/themes/thub/images/avi.jpg)