[Solved]1. I got a car that I’ve made and a script I found that I’ve tweaked a little, but the problem is that the car isnt doing what I want it to.
It is fairly easy to handle at low speeds but if it goes a little faster than that it flips as soon as I turn a little to one side.
I read somewhere about changing gravity might fix it but I dunno how.
also, the turning speed is like same as the torque which isnt what I want, fast turning and maby not as fast for moving.(should be changeable)
2.Now the car turns right when I try to go forward in a straight line and left when I go backwards,
code:
if(speed > maxSpeed){
RearLeftWheel.motorTorque = 0;
RearRightWheel.motorTorque = 0;
}else{
if(speed != 0)
throttle = Input.GetAxis("Vertical");
else
throttle = Input.GetAxis("Vertical") - 0.3;
FrontLeftWheel.motorTorque = EngineTorque * throttle;
FrontRightWheel.motorTorque = EngineTorque * throttle;
//I tried fixing with this but didnt help
FrontLeftWheel.motorTorque = FrontRightWheel.motorTorque;
FrontRightWheel.motorTorque = FrontLeftWheel.motorTorque;
}
FrontLeftWheel.steerAngle = 20 * Input.GetAxis("Horizontal");
FrontRightWheel.steerAngle = 20 * Input.GetAxis("Horizontal");
EDIT:
Use This Demo to see how the car behaves if it helps(Tip, use F to see each wheels rpm)
I find the turning most noticable at low speeds too.
Put this on your car and place script in a java script. and add a rigged body to you car setup and you are done. hope this works for you.
var rearWheel1 : WheelCollider;
var rearWheel2 : WheelCollider;
var frontWheel1 : WheelCollider;
var frontWheel2 : WheelCollider;
var wheelFL : Transform;
var wheelFR : Transform;
var wheelML : Transform;
var wheelMR : Transform;
var wheelRL : Transform;
var wheelRR : Transform;
var steer_max = 20;
var motor_max = 40;
var brake_max = 100;
var steerSpeed = 20;
private var steer = 0;
private var forward = 0;
private var back = 0;
private var brakeRelease = false;
private var motor = 0;
private var brake = 0;
private var reverse = false;
private var speed = 0;
function Start() {
rigidbody.centerOfMass = Vector3(0, -0.05, 0);
}
function FixedUpdate () {
speed = rigidbody.velocity.sqrMagnitude;
steer = Input.GetAxis("Horizontal");
forward = Mathf.Clamp(Input.GetAxis("Vertical"), 0, 1);
back = -1 * Mathf.Clamp(Input.GetAxis("Vertical"), -1, 0);
if(speed == 0 && forward == 0 && back == 0) {
brakeRelease = true;
}
if(speed == 0 && brakeRelease) {
if(back > 0) { reverse = true; }
if(forward > 0) { reverse = false; }
}
if(reverse) {
motor = -1 * back;
brake = forward;
} else {
motor = forward;
brake = back;
}
if (brake > 0 ) { brakeRelease = false; };
rearWheel1.motorTorque = motor_max * motor;
rearWheel2.motorTorque = motor_max * motor;
rearWheel1.brakeTorque = brake_max * brake;
rearWheel2.brakeTorque = brake_max * brake;
if ( steer == 0 && frontWheel1.steerAngle != 0) {
if (Mathf.Abs(frontWheel1.steerAngle) <= (steerSpeed * Time.deltaTime)) {
frontWheel1.steerAngle = 0;
} else if (frontWheel1.steerAngle > 0) {
frontWheel1.steerAngle = frontWheel1.steerAngle - (steerSpeed * Time.deltaTime);
} else {
frontWheel1.steerAngle = frontWheel1.steerAngle + (steerSpeed * Time.deltaTime);
}
} else {
frontWheel1.steerAngle = frontWheel1.steerAngle + (steer * steerSpeed * Time.deltaTime);
if (frontWheel1.steerAngle > steer_max) { frontWheel1.steerAngle = steer_max; }
if (frontWheel1.steerAngle < -1 * steer_max) { frontWheel1.steerAngle = -1 * steer_max; }
}
frontWheel2.steerAngle = frontWheel1.steerAngle;
wheelFL.localEulerAngles.y = frontWheel1.steerAngle;
wheelFR.localEulerAngles.y = frontWheel2.steerAngle;
wheelFR.Rotate(0, 0, frontWheel1.rpm * -6 * Time.deltaTime);
wheelFL.Rotate(0, 0, frontWheel2.rpm * -6 * Time.deltaTime);
wheelMR.Rotate(0, 0, rearWheel1.rpm * -6 * Time.deltaTime);
wheelML.Rotate(0, 0, rearWheel2.rpm * -6 * Time.deltaTime);
wheelRR.Rotate(0, 0, rearWheel1.rpm * -6 * Time.deltaTime);
wheelRL.Rotate(0, 0, rearWheel2.rpm * -6 * Time.deltaTime);
}
Yes, the wheels need to be “pointed” in the correct direction. If the car is spinning, left wheels are not going the same direction as the right wheels. I wouldnt bother with trying to rotate the geometry wheels until all of the physics are correct.
It’s easy to have way too much torque, balancing the mass and tork is important to get realistic effects.
The trick listed above to lower the center of gravity is very important, otherwise your center of gravity is the geometric center of your collider. A real car is very dense at the botom…
Also, when you drive at a high speed in real life, the centrifugal force of the wheels makes it tough to steer and adds stability (like a bicycle). In my driving game, I make steering less effective at high speeds, this decreases the car flipping.
Well you can change the gravity in Edit->Project->Physics but I don’t think that’s what you need to do. Try to lower the torque of the wheels by adding mass to the rigidbody, i would also adjust drag to suit and get rid of angular drag to stop the ice skating feeling.
What I like to do with cars is actually apply a downward force to the rigidbody (to act as wind downforce). The Unity wheel colliders seem to the like this too and if you adjust the center of gravity with this force when you turn you can get some really nice effects (and handling) when turning.
Good luck! Best advice I can give is think about a real car, and what unity physics you can apply to your rigidcar to emulate.
You may have too much EngineRPM and EngineTorque also if you have a rigidbody you should set the mass to 1500 that is a normal cars mass.
Not sure if it will help, but the front wheels on real cars are turned very slightly inward at the front. This is called “toe in” and is the reason why your steering wheel straightens out when you’re moving forward and let the wheel turn on its own. That might help with the steering drift problem you’re having. I couldn’t get the car to flip when I turned at up to 60mph - but I did some sweet skids. Honestly the best five minutes of my day so far.