Motorbike Physics

Ok,

here is my motorbike script:

#pragma strict

var speed : float = 2;

private var power : float = 0.0;
var enginePower : float = 250;

private var steer : float = 0.0;
var maxSteer : float = 25;

private var brake = 0;

var backWheel : WheelCollider;
var frontWheel : WheelCollider;

var COM : Vector3 = Vector3(0, -0.5, 0.3);

function Start()
{
    rigidbody.centerOfMass=COM;
}

function FixedUpdate()
{
    power=Input.GetAxis("Vertical") * enginePower * Time.deltaTime * 250.0;
    steer=Input.GetAxis("Horizontal") * maxSteer;
	brake=Input.GetKey("space") ? rigidbody.mass * 0.1: 0.0;

	frontWheel.steerAngle = steer;
	
	if(brake > 0.0)
	{
        backWheel.brakeTorque = 0;
        frontWheel.brakeTorque = 0;
    } else {
        backWheel.motorTorque = power;
        frontWheel.motorTorque = power;
    }
}

function OnDrawGizmos()
{
	Gizmos.color = Color.red;
	Gizmos.DrawSphere (COM, 0.1);
}

However, when speeding up it sometimes does a wheelie, and turning makes it flip over, etc. etc. anyone got any ideas?

Without deep looking into your code I perfectly know the problem. It’s matter of right ballance which is sensitive. It’s the issue of all hi-power two-wheeled vehicles as they’ve got their center of mass placed high comparing to cars. Resolve it the way it’s done by motorcycle drivers. They apply as much throttle as it’s necessary without making a wheelie which is wasting of grip power (center of mass goes higher and higher so to not flip the machine driver has to apply constantly less throttle - this way bike can’t accelerate that fast or we’re screwed on the ground). You have to observe the angle of bike flip and proportionally cut the power applied to rear wheel (or braking power on front wheel to avoid crash stoppy). To get rid of power application which leads to oscillations you have also to observe angular velocity of flipping machine. It results in classic PD controller problem.

Tom

P.S. Bike physics are exciting one, but going into details is out of scope for forum I guess (I’m doing my bike experiments in Unity for years now). You can easily find more about it looking on the net for Tony Foale’s articles or Cossalter (I believe I don’t misspelling this name).