Created
February 22, 2014 18:01
-
-
Save RandomOutput/9159059 to your computer and use it in GitHub Desktop.
Unity c# code for car controls.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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