Traffic Cars Spawning Problem

Hello Guys,

I’m having some trouble in instantiating cars in an endless car game like Traffic Racer.
I’m trying to spawn my traffic cars at Last Tile of my TilesPool. Cars are spawning fine, but they
spawn at same position some times and due to rigidbody they start flying. I want to avoid this . I want the cars to be instantiated at different location on z.

private IEnumerator Start()
    {
        _player = GameObject.FindGameObjectWithTag(Tags.player);
        LastGeneratedCar = _pathGenerator.LastTile;

        while (true)
        {
            GenerateTraffic();
            yield return new WaitForSeconds(Random.Range(minValue, maxValue));
            
        }
    }

private void GenerateTraffic()
{

    if (carRandomValue != _prevCarRandomVal)
    {
        _prevCarRandomVal = carRandomValue;
        var tempCar = Instantiate(DummyCars[carRandomValue],
            new Vector3(TrafficRandomPositions[Random.Range(0, TrafficRandomPositions.Length)],                  // I've four traffic random position assigned from inspector
                _pathGenerator.LastTile.transform.position.y,
                _pathGenerator.LastTile.transform.position.z + CarInstantiationOffset ), Quaternion.identity) as GameObject;
        TrafficIterationCount++;
    }
    else
    {

// Debug.Log(“Same Value Generated”);
GenerateTraffic();
}

Traffice car that is instantiated is moving along the z axis too. I’ve used rigidbody.MovePosition in my FixedUpdate of the traffic car.

Please help me out with this. I’ve been banging my head for some time on this. I know there’d be an easy fix but I’m unable to find that.

Thanks in Advance

You should keep track of _prevTrafficPositionRandomValue, not only _prevCarRandomValue, and make sure you don’t generate the same traffic position twice in a row. If they move away faster than the minValue you use in WaitForSeconds, you should be safe.