I’ve been working on a racing game using scripts from the car tutorial demo and my own scripts and need some help with a C# script. Right now the MPH displays as MPH: 123.4534534 and I would like help formatting it to MPH: 2 (without decimals). If some one could be kind enough to help that would be greatly appreciated.
using UnityEngine;
using System.Collections;
// This class is repsonsible for controlling inputs to the car.
// Change this code to implement other input types, such as support for analogue input, or AI cars.
[RequireComponent (typeof (Drivetrain))]
public class CarController : MonoBehaviour {
public GUISkin skin;
// Add all wheels of the car here, so brake and steering forces can be applied to them.
public Wheel[] wheels;
// A transform object which marks the car's center of gravity.
// Cars with a higher CoG tend to tilt more in corners.
// The further the CoG is towards the rear of the car, the more the car tends to oversteer.
// If this is not set, the center of mass is calculated from the colliders.
public Transform centerOfMass;
// A factor applied to the car's inertia tensor.
// Unity calculates the inertia tensor based on the car's collider shape.
// This factor lets you scale the tensor, in order to make the car more or less dynamic.
// A higher inertia makes the car change direction slower, which can make it easier to respond to.
public float inertiaFactor = 1.5f;
// current input state
float brake;
float throttle;
float throttleInput;
float steering;
float lastShiftTime = -1;
float handbrake;
Drivetrain drivetrain;
public float shiftSpeed = 0.8f;
public float throttleTime = 1.0f;
public float throttleTimeTraction = 10.0f;
public float throttleReleaseTime = 0.5f;
public float throttleReleaseTimeTraction = 0.1f;
public bool tractionControl = true;
public float steerTime = 1.2f;
public float veloSteerTime = 0.1f;
public float steerReleaseTime = 0.6f;
public float veloSteerReleaseTime = 0f;
public float steerCorrectionFactor = 4.0f;
public float slipVelo {
get {
float val = 0.0f;
foreach(Wheel w in wheels)
val += w.slipVelo / wheels.Length;
return val;
}
}
// Initialize
void Start ()
{
if (centerOfMass != null)
rigidbody.centerOfMass = centerOfMass.localPosition;
rigidbody.inertiaTensor *= inertiaFactor;
drivetrain = GetComponent (typeof (Drivetrain)) as Drivetrain;
}
void Update ()
{
// Steering
Vector3 carDir = transform.forward;
float fVelo = rigidbody.velocity.magnitude;
Vector3 veloDir = rigidbody.velocity * (1/fVelo);
float angle = -Mathf.Asin(Mathf.Clamp( Vector3.Cross(veloDir, carDir).y, -1, 1));
float optimalSteering = angle / (wheels[0].maxSteeringAngle * Mathf.Deg2Rad);
if (fVelo < 1)
optimalSteering = 0;
float steerInput = 0;
if (Input.GetKey (KeyCode.LeftArrow))
steerInput = -1;
if (Input.GetKey (KeyCode.RightArrow))
steerInput = 1;
if (steerInput < steering)
{
float steerSpeed = (steering>0)?(1/(steerReleaseTime+veloSteerReleaseTime*fVelo)) :(1/(steerTime+veloSteerTime*fVelo));
if (steering > optimalSteering)
steerSpeed *= 1 + (steering-optimalSteering) * steerCorrectionFactor;
steering -= steerSpeed * Time.deltaTime;
if (steerInput > steering)
steering = steerInput;
}
else if (steerInput > steering)
{
float steerSpeed = (steering<0)?(1/(steerReleaseTime+veloSteerReleaseTime*fVelo)) :(1/(steerTime+veloSteerTime*fVelo));
if (steering < optimalSteering)
steerSpeed *= 1 + (optimalSteering-steering) * steerCorrectionFactor;
steering += steerSpeed * Time.deltaTime;
if (steerInput < steering)
steering = steerInput;
}
// Throttle/Brake
bool accelKey = Input.GetKey (KeyCode.UpArrow);
bool brakeKey = Input.GetKey (KeyCode.DownArrow);
if (drivetrain.automatic drivetrain.gear == 0)
{
accelKey = Input.GetKey (KeyCode.DownArrow);
brakeKey = Input.GetKey (KeyCode.UpArrow);
}
if (Input.GetKey (KeyCode.LeftShift))
{
throttle += Time.deltaTime / throttleTime;
throttleInput += Time.deltaTime / throttleTime;
}
else if (accelKey)
{
if (drivetrain.slipRatio < 0.10f)
throttle += Time.deltaTime / throttleTime;
else if (!tractionControl)
throttle += Time.deltaTime / throttleTimeTraction;
else
throttle -= Time.deltaTime / throttleReleaseTime;
if (throttleInput < 0)
throttleInput = 0;
throttleInput += Time.deltaTime / throttleTime;
brake = 0;
}
else
{
if (drivetrain.slipRatio < 0.2f)
throttle -= Time.deltaTime / throttleReleaseTime;
else
throttle -= Time.deltaTime / throttleReleaseTimeTraction;
}
throttle = Mathf.Clamp01 (throttle);
if (brakeKey)
{
if (drivetrain.slipRatio < 0.2f)
brake += Time.deltaTime / throttleTime;
else
brake += Time.deltaTime / throttleTimeTraction;
throttle = 0;
throttleInput -= Time.deltaTime / throttleTime;
}
else
{
if (drivetrain.slipRatio < 0.2f)
brake -= Time.deltaTime / throttleReleaseTime;
else
brake -= Time.deltaTime / throttleReleaseTimeTraction;
}
brake = Mathf.Clamp01 (brake);
throttleInput = Mathf.Clamp (throttleInput, -1, 1);
// Handbrake
handbrake = Mathf.Clamp01 ( handbrake + (Input.GetKey (KeyCode.Space)? Time.deltaTime: -Time.deltaTime) );
float shiftThrottleFactor = Mathf.Clamp01((Time.time - lastShiftTime)/shiftSpeed);
drivetrain.throttle = throttle * shiftThrottleFactor;
drivetrain.throttleInput = throttleInput;
if(Input.GetKeyDown(KeyCode.A))
{
lastShiftTime = Time.time;
drivetrain.ShiftUp ();
}
if(Input.GetKeyDown(KeyCode.Z))
{
lastShiftTime = Time.time;
drivetrain.ShiftDown ();
}
foreach(Wheel w in wheels)
{
w.brake = brake;
w.handbrake = handbrake;
w.steering = steering;
}
}
void OnGUI ()
{
if (skin != null) {
GUI.skin = skin;
}
GUI.Box (new Rect(0,460,100,30),"mph:"+rigidbody.velocity.magnitude * 2.237f);
// would like to format MPH as whole numbers rounding up.
}
}