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);
}