3 roads infinite runner problem of player movement

Hello !

First I hope that I’m in the good place to ask, if not tell me.

I want to create a simple 3D runner.

But I have a problem, about the player movement because I have 3 roads (blue, green, red), and I want that when the player swipe (or click) he move only from one road to another. Consequently this is not free movements.

My initial idea was to assign a number to each road : | -1 | 0 | 1 |. And create some conditions if you’re on 1 and you click on right you will not move because there is no road after the red one.

However it doesn’t work very well.
Picture :

  // If we click to left and the position of the player is 0 or 1

        if (Input.GetKey("left") && (playerPosition == 0 || playerPosition == 1))
        {
           
            // Change direction to left
                endPosition = new Vector3(transform.position.x - 1, transform.position.y, transform.position.z);
            playerPosition = playerPosition - 1;
           
            Debug.Log("playerPosition Left : " + playerPosition);

        }
        if (Input.GetKey("right") && (playerPosition == -1 || playerPosition == 0))
        {

           
            // Change direction to right
            endPosition = new Vector3(transform.position.x + 1, transform.position.y, transform.position.z);
            playerPosition = playerPosition + 1;
            Debug.Log("playerPosition Right : " + playerPosition);
        }
       

        gameObject.transform.position = Vector3.Lerp(direction, endPosition, speed);
       
      

    }

}

Do you have some advices about how to move this player in relation to the road ?

Thanks and have a good day !

First of all, Input.GetKey registers true on each frame the key is down. This means that to hit the center lane with your code, you’ll have to hit the key for exactly one frame - which is almost impossible.

Use GetKeyDown, which only returns true on the first frame after the key was pressed, that’ll help with that.

Your position code is also all wrong - you’re lerping from direction to endPosition, which makes no sense - I don’t think you understand what Lerp does. A good article about what Lerp is, and how to use it, is the “lerp like a pro” article linked in my signature.

Also Multiply speed by Time.deltaTime for smooth movement on all systems ( framerate independent )