# Endless runner distance calculation problem

I have a bit of a problem: see, i want my endless runner game to spawn in a checkpoint(3d object) each time the distance (transform position- start position) is over 500 meters. I have a basic understanding and this is the script i wrote.

``````Public int distance;
Public int increment = 500;//value the distance needs to be over for a checkpoint to spawn.
Public int counter;
Public int current;

Update()
{
Distance = (origin.position- transform.position ).magnitude

counter = distance/increment;

if (distance > increment)
{
Current++;
}

If (current > last)
{
Last = current;
Print("dist" + (counter*100) + "meters);
//this is where the checkpoint should spaen
//this is also where the problem is.

}

}
``````

The problem is, the print function gets called EVERY FRAME. i want it to print ONLY ON the first frame, and then cease it from printing until it needs to again.
Any help is greatly appreciated

Wow it looks like you’re trying something really weird, for something you can do for way more simple and more easy to understand.

1. Please display the real code you are using, not the shortened one (no code like this would works in Unity as it).

2. There are missing info in this example, “last” is not defined before it’s used and does the “origin” or “transform” object moves ? We don’t know. I suppose the transform moves (in another script).

3. By testing your code with debug logs, you could find by yourself where is the error, then fix it.

4. Stocking distance between moving objects in a int, that’s weird.

5. I’ll answer you with C#. I suppose that the script is on the player, which is moving and the “origin” is the spawnpoint of the player, which isn’t moving.

Put this inside your class :

``````//Don't forget to assign it
public Transform origin;

public float distanceBetweenCheckpoints = 500f;
float currentDistanceToNextCheckpoint;
int numberOfCheckpointsCrossed;

//Don't forget to write some code to move your transform position
void Update ()
{
//Calculate distance
float totalDistance = Vector3.Distance (origin.position, transform.position);

//Does a modulo operation
currentDistanceToNextCheckpoint = totalDistance % (float) numberOfCheckpointsCrossed;

//Check if the distance is superior than a certain amount
if (currentDistanceToNextCheckpoint >= distanceBetweenCheckpoints);
numberOfCheckpointsCrossed ++;
Debug.Log ("Cross a checkpoint");
}
``````

if (distance > increment)

After you have traveled over 500 units, this will be true every frame so what you are describing is expected behaviour.

You have to change your code a bit

``````public int distance;
public int increment = 500;//value the distance needs to be over for a checkpoint to spawn.
public int counter;
public int current;

void Update()
{
distance = (int)(origin.position- transform.position ).magnitude
// How many times 500 units you have travelled
counter = distance/increment;

// You have traveled 500 units more times than last frame
if (counter > current)
{
// Remember the new amount
current = counter;
// do everything related to reaching a new 500 unit milepost here
}
``````