Question by
goutham12 · Jul 29, 2016 at 01:56 AM ·
speedcar physics
simple car controle
am using one script for my car movement. that was downloaded from Internet. in that i can't increase the speed to my vehicles. this is my script
using UnityEngine; using UnityEngine.UI; using System.Collections;
public class CarController : MonoBehaviour {
public float idealRPM = 500f;
public float maxRPM = 1000f;
public Transform centerOfGravity;
public WheelCollider wheelFR;
public WheelCollider wheelFL;
public WheelCollider wheelRR;
public WheelCollider wheelRL;
public float turnRadius = 6f;
public float torque = 25f;
public float brakeTorque = 100f;
public float AntiRoll = 20000.0f;
public enum DriveMode { Front, Rear, All };
public DriveMode driveMode = DriveMode.Rear;
public Text speedText;
Rigidbody body;
void Start() {
body = GetComponent<Rigidbody> ();
body.centerOfMass = centerOfGravity.localPosition;
}
public float Speed() {
return wheelRR.radius * Mathf.PI * wheelRR.rpm * 60f / 1000f;
}
public float Rpm() {
return wheelRL.rpm;
}
void FixedUpdate () {
if(speedText!=null)
speedText.text = "Speed: " + Speed().ToString("f0") + " km/h";
//Debug.Log ("Speed: " + (wheelRR.radius * Mathf.PI * wheelRR.rpm * 60f / 1000f) + "km/h RPM: " + wheelRL.rpm);
float scaledTorque = Input.GetAxis("Vertical") *torque;
if(wheelRL.rpm < idealRPM)
scaledTorque = Mathf.Lerp(scaledTorque/10f, scaledTorque, wheelRL.rpm / idealRPM );
else
scaledTorque = Mathf.Lerp(scaledTorque, 0, (wheelRL.rpm-idealRPM) / (maxRPM-idealRPM) );
DoRollBar(wheelFR, wheelFL);
DoRollBar(wheelRR, wheelRL);
wheelFR.steerAngle = Input.GetAxis("Horizontal") * turnRadius;
wheelFL.steerAngle = Input.GetAxis("Horizontal") * turnRadius;
wheelFR.motorTorque = driveMode==DriveMode.Rear ? 0 : scaledTorque;
wheelFL.motorTorque = driveMode==DriveMode.Rear ? 0 : scaledTorque;
wheelRR.motorTorque = driveMode==DriveMode.Front ? 0 : scaledTorque;
wheelRL.motorTorque = driveMode==DriveMode.Front ? 0 : scaledTorque;
if(Input.GetButton("Fire1")) {
wheelFR.brakeTorque = brakeTorque;
wheelFL.brakeTorque = brakeTorque;
wheelRR.brakeTorque = brakeTorque;
wheelRL.brakeTorque = brakeTorque;
}
else {
wheelFR.brakeTorque = 0;
wheelFL.brakeTorque = 0;
wheelRR.brakeTorque = 0;
wheelRL.brakeTorque = 0;
}
}
void DoRollBar(WheelCollider WheelL, WheelCollider WheelR) {
WheelHit hit;
float travelL = 1.0f;
float travelR = 1.0f;
bool groundedL = WheelL.GetGroundHit(out hit);
if (groundedL)
travelL = (-WheelL.transform.InverseTransformPoint(hit.point).y - WheelL.radius) / WheelL.suspensionDistance;
bool groundedR = WheelR.GetGroundHit(out hit);
if (groundedR)
travelR = (-WheelR.transform.InverseTransformPoint(hit.point).y - WheelR.radius) / WheelR.suspensionDistance;
float antiRollForce = (travelL - travelR) * AntiRoll;
if (groundedL)
body.AddForceAtPosition(WheelL.transform.up * -antiRollForce,
WheelL.transform.position);
if (groundedR)
body.AddForceAtPosition(WheelR.transform.up * antiRollForce,
WheelR.transform.position);
}
}
check it.... help me
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612085733im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to revert speed back to normal after changing with Key. 1 Answer
Something is wrong with my max. speed code 0 Answers
Increase variable? 0 Answers
Can a csv file be used to change a player speed? 0 Answers
Virtual Joystick Player Speed Problem 0 Answers