Skip to content

Instantly share code, notes, and snippets.

@RandomOutput
Created February 22, 2014 18:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RandomOutput/9159059 to your computer and use it in GitHub Desktop.
Save RandomOutput/9159059 to your computer and use it in GitHub Desktop.
Unity c# code for car controls.
using UnityEngine;
using System.Collections;
public class CarControls : MonoBehaviour {
public float rotationRange = 1.0f;
public float minSpeed = 0.0f;
public float maxSpeed = 20.0f;
public float carSpeed = 10.0f;
private float carRotation = 0.0f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
//Rotation
float mousePos = UnityEngine.Input.mousePosition.x;
mousePos /= Screen.width; //Get 0 to 1 unit space.
mousePos -= 0.5f; //Center unit space.
carRotation += mousePos * 2.0f * rotationRange; //Expand to rotational range.
gameObject.transform.rotation = Quaternion.Euler(new Vector3(0.0f, carRotation, 0.0f)); //Rotate object.
//carSpeed = minSpeed + (yPosition * (maxSpeed-minSpeed));
gameObject.transform.position += gameObject.transform.forward * carSpeed * Time.deltaTime;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment