I’m trying to spawn tubular objects based on the distance my player object has traveled. Similar to how Flappy Bird places tubes in its game as the player moves in the x direction. I’ve created prefabs to Instantiate the tubes when needed. I’m calculating the distance my player has traveled with the following code. If I want to spawn such tubes, say for every 5 units my player moves in the x direction, how would I go about this? Could I use distanceTraveled % 5 and then Instantiate the objects based on that? Thanks.
float distanceTraveled = 0;
Vector3 lastPosition;
void Start()
{
lastPosition = transform.position;
}
void Update()
{
distanceTraveled += Vector3.Distance(transform.position, lastPosition);
lastPosition = transform.position;
}