Simple 3D Runner physics

So the user will constantly run forward. That is working. (I know I should be using addforce instead of translate but that doesn’t matter in this case.) So the user also needs to be able to jump, and “Switch lanes” which have been previously set positions. I had it working but then i had to add a rigidbody for the jump functionality and that screwed things up. Any advice on how to get this function working? The switching lanes I mean. Here is my terrible script.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class AutoRun : MonoBehaviour {

    public float speed = 2.0f;
    public Vector3 jumpVelocity;
    private List<Vector3> Lane = new List<Vector3>();

    private bool grounded = true;
  
    void Awake()
    {
        Lane.Add(new Vector3(-0.6f, 0, 0));
        Lane.Add(new Vector3(0.0f, 0, 0));
        Lane.Add(new Vector3(+0.6f, 0, 0));
        this.gameObject.transform.position = Lane[1];
    }
	// Update is called once per frame
	void Update () 
    {
        //continuously move forward.
        transform.Translate(Vector3.forward * speed * Time.deltaTime);
        
        //Allow jumping
        if (grounded && Input.GetKeyDown(KeyCode.Space))
        {
            rigidbody.AddForce(jumpVelocity, ForceMode.VelocityChange);
        }


        //LaneSwitching
        if(Input.GetKeyDown(KeyCode.A))
        {
            //Move Right
            Debug.Log("Move Left");
            
            //if we are in the right lane
            if(this.gameObject.transform.position.x > Lane[1].x)
            {
                //This is the last method i tried, i only did it once to see if it would work but it didn't.
                this.gameObject.transform.position = new Vector3(transform.position.x + Lane[1].x, transform.position.y, transform.position.z);
            }//if we are in the left lane
            else if(this.gameObject.transform.position.x < Lane[1].x)
            {
                //do nothing...
            }//if we are in the middle lane
            else if(this.gameObject.transform.position.x == Lane[1].x)
            {
                this.gameObject.transform.position = Lane[0];
            }
        }
        if (Input.GetKeyDown(KeyCode.D))
        {
            //Move Right
            Debug.Log("Move Right");

            //if we are in the right lane
            if (this.gameObject.transform.position.x > Lane[1].x)
            {
                //Do nothing...
            }//if we are in the left lane
            else if (this.gameObject.transform.position.x < Lane[1].x)
            {
                this.gameObject.transform.position = Lane[1];
            }//if we are in the middle lane
            else if (this.gameObject.transform.position.x == Lane[1].x)
            {
                this.gameObject.transform.position = Lane[2];
            }
        }
	}
}

If the rigidbody screws up your movement, you could just remove it and use transform.translate to simulate jumping. If your player does have any colliders/rigidbody, you can use raycasts to detect obstacles and cause the player to react accordingly.

This code ok. tranform 3 lan
"
code for you fall
if(Input.GetKeyDown(KeyCode.A))
{
//Move Right
Debug.Log(“Move Left”);

         //if we are in the right lane
         if(this.gameObject.transform.position.x > Lane[1].x)
         {
             //This is the last method i tried, i only did it once to see if it would work but it didn't.
             this.gameObject.transform.position = new Vector3(transform.position.x + Lane[1].x, transform.position.y, transform.position.z);
         }//if we are in the left lane
         else if(this.gameObject.transform.position.x < Lane[1].x)
         {
             //do nothing...
         }//if we are in the middle lane
         else if(this.gameObject.transform.position.x == Lane[1].x)
         {
             this.gameObject.transform.position = Lane[0];
         }
     }

"
This great
1.using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class CubeTest2 : MonoBehaviour {

public float speed = 2.0f;
public Vector3 jumpVelocity;
private List<Vector3> Lane = new List<Vector3>();
private bool grounded = true;
// Use this for initialization
void Awake()
{
    Lane.Add(new Vector3(-10, 0, 0));
    Lane.Add(new Vector3(0,0,0));
    Lane.Add(new Vector3(10, 0, 0));
    this.gameObject.transform.position = Lane[1];
}
void Start () {

}

// Update is called once per frame
void Update () {
    transform.Translate(Vector3.forward * speed * Time.deltaTime);
    if (grounded && Input.GetKeyDown(KeyCode.Space))
    {
        rigidbody.AddForce(jumpVelocity, ForceMode.VelocityChange);
    }
    //Lane Swithching
    if (Input.GetKeyDown(KeyCode.LeftArrow))
    {
      
            if (this.transform.position.x < Lane[1].x)
            {
            //Nothing
        }
        else
                if (this.gameObject.transform.position.x > Lane[1].x)
                {
                this.gameObject.transform.position = Lane[1];
            }
            else
                if (this.gameObject.transform.position.x == Lane[1].x)
                {
                    this.gameObject.transform.position = Lane[0];
                }
    }
    if (Input.GetKeyDown(KeyCode.RightArrow))
    {
        if (this.gameObject.transform.position.x > Lane[1].x)
        {
            //Nothing
        }
        else
            if (this.transform.position.x < Lane[1].x)
            {
                this.gameObject.transform.position = Lane[1];
            }
        else
                if (this.gameObject.transform.position.x == Lane[1].x)
                {
                    this.gameObject.transform.position = Lane[2];
                }
    }

}

}

  1. List item