- Home /
Infinite runner world creation
Hey people,can someone help me on scripting this.I'm doing this like subway surfer,keeping player stationary and moving the world around him.My question is how can i instantiate the track prefab continuously right after previous one..?
For instance I have track named Track_A as prefab and as soon as this track is instantiated in the world, a script attached to it will make it move.I instantiate 5 prefabs at start-up one after another tailing perfectly.
When the first track is destroyed after reaching a certain point,it will get destroyed and a new track will be instantiated at the end of the chain but will cause FLOATING POINT PRECISION ERRORS leaving small gap between track prefabs.HOW CAN I SOLVE THIS..?
I am attaching the script with this
TrackScript
//The length of the track (will be predefined and same for all track)
public float tracklength=10;
//The speed at which the tracks are moving
private float worldspeed=10.0f;
void Update()
{
//Move the new track towards the camera
transform.translate(-Vector3.forward*Time.deltatime*worldspeed);
}
//Function to get the end of this track to create the next track
public Vector3 GetTrackEndPos()
{
Vector3 Endposition=transform.position;
Endposition.z+=tracklength;
return Endposition;
}
WorldScript
//This function will be called when a track at player end is destroyed to create a new track ahead of the last track
public void CreateOneTrackAhead()
{
if(LastTrackPrefab)
{Pos_TrackInstantiate=LastTrackPrefab.GetComponent<TrackScript>().GetTrackEndPos();}
//Instantiate the track at the end of previous track
LastTrackPrefab=Instantiate(TrackPrefab,Pos_TrackInstantiate,Quaternion.identity);
}
Answer by iamvishnusankar · Feb 11, 2014 at 07:51 PM
Use two cubes for each track; one as entry trigger and one as exit trigger "AND TURN OFF MESH RENDERING " to make the cube invisible on scene. Loop the sequence as shown in picture.
The reason exit trigger of Track A is set to the middle of Track B is to avoid the viewing problem. As it may leave a gap on destroyed area which might be annoying to viewer
Credits : MS Paint :D :D
void OnTriggerEnter(collider other)
{
//logic to instantiate the next track
}
void OnTriggerExit(collider other) {
//logic to destroy the previous track }
Thanks for ur reply.Your solution was not exactly perfect however advising the box colliders were helpful.I managed to make it using one box collider as trigger per track. Thank and Cheers..
Sethuraj, how was you able to create the script? I plan to do the same with box colliders and as iamvishnusankar's answer was really helpful, I am still unable to get my head around the coding.
Check this question. This might be helpful for you.
[ prefab generator in C# for endless runner[Solved]][1]
[1]: http://answers.unity3d.com/questions/637863/creating-a-random-prefab-generator-in-c-for-endles.html
Just call the CreatePrefab() (Check the above link to know what it is)function as soon as you enter/exit trigger :)
I am jumping into this thread with a question, hope that is O$$anonymous$$. sethuraj is stating "moving the world around him", why is that and not moving the runner? Is it to save space in the world terrain?
We'd prefer them as a new question with a reference or as a comment not as an "Answer" which means "Solution" on UA - I have converted it for you this time.
Answer by Kiloblargh · Mar 03, 2014 at 05:49 AM
I am surprised you would see floating point precision errors in such a short distance.
If that's really going on, how about using a double
?
Your real problem is probably caused by moving it by a factor of Time.deltaTime every frame, which is a lot more error-prone than moving it a fraction of a set distance relative to the elapsed time since the last reset.
Your answer
![](https://koobas.hobune.stream/wayback/20220613133218im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How can I make my animation to cover a particular distance? 0 Answers
Infinite runner, insert customized text inside coins and obstacles 0 Answers
How to create a "save me" feature like subway surfers .temple run, etc ? 0 Answers
Object Pooling: deactivating vs. moving 1 Answer
I have a problem with the infinite runner tut by mike geig 1 Answer