How to restict an object to move in 3 paths on one axis?

I am fairly new to Unity so people please if I say something stupid forgive me. I have my character and I want him to move in 3 virtual lines. More specific I want to restrict the character e.g. to move only on x axes on -5, 0, +5 and not in the betwween values. Because I know I cannot be more specific I will tell you an example. I want to have a mechanism similar to the game “Subway Surfers” where the character is forced to run in one of the three paths. Thanks a lot in advance!

As you are a fresher I advice you to go through some basic stuffs before getting into something big like runner games.Subway surfer use more complex system like bending the whole world though a Geometry level shader to achive the curves.

For now as a simple example,this will help you get started.You can achieve this by moving the player(say try with a capsule) in forward direction always while offseting/Lerping the X position to -5,0,+5.

void Update()
   //This will move the player always in forward with given speed

void SwitchLane(float Offset)
   //Get current position	   	
   Vector3 pos=transform.position;
  //Modify the X value with offset		
  //Reassign the new position		


call the SwitchLane() function with 5 or 0 or -5 as offset value argument to switch the lane.This will immediately change the position to one track to wont get smooth transition.The player will always run straight,no curves or turnings.This is just a simple example.

Hope this helps…!!!

In many of these games, while it looks like the character is moving, what really happens is that the character and the camera hang around the origin (never moving on the ‘z’ axis), and all the obstacles and terrain is moved towards the the player. This kind of setup can simplify a number of problems. Given this type of setup, you can script your motion as:

var speed : float = 3.0;
var positions : Vector3[];

private var index = 1;
function Update()  {

    if (Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.A))  {
    	if (index < 0) index = 0;
    if (Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown("d")) {
    	if (index >= positions.Length) index = positions.Length-1;
 	transform.position = Vector3.MoveTowards(transform.position, positions[index], Time.deltaTime * speed);

Test this script in a new scene with just a block at the origin to start. After attaching the script, you have to setup ‘positions’ in the inspector. Select ‘positions’, click on the triangle to open the array, set the size to 3, and set the three entries as (-1,0,0), (0,0,0), (1,0,0) to start.