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];
}
}
}
}