Hi Guys,
I am working on bike race game (M also new in unity game development) I go with car tutorial of unity, My bike move smooth up to a speed (you can get it like at 100 KM/h) I am applying torque to back wheel and bike moves smooth. After a specific speed my bike also getting jerks and then it will become up from back side and runs only on front wheel (just like bike stunt)
I put centerOfMass of rigid-body back side of bike and it’s move smoth but bike leans automatic after a speed. and even moving center of mass to back side is not an good idea.
here with i share my code
using UnityEngine;
using System.Collections;
public class BikeMove : MonoBehaviour {
public const float PI = 3.1415f;
public WheelCollider frontWheel;
public WheelCollider rearWheel;
public float gearRatio = {4.31f, 3.11f, 1.88f, 1.41f, 1.13f, 1.0f};
private int intCurrentGear = 0;
private float torqueRatio = 0f;
private bool isBrake = false;
private float fltHandBrake = 1.0f;
private float engineTorque = 0.0f;
public float maxEngineRPM = 3000f;
private float engineRMP = 0f;
//private float bikeHP = 0f;
public int acceleration =1;
public GUIText guiSpeed;
public GUIText guiGear;
public GUIText guiRPM;
private float speed = 0f;
public float maxSpeed = 140;
public float mass = 250.0f;
public int maxSteer = 18;
public float turnAngle = 0F;
public float gyroscope = 0.0f;
public float gyrosteer = 0.0f;
public float steerLean = 0.0f;
public float leanFactor = 0.0f;
private Vector3 relativeVelocity = Vector3.zero;
private float inputY = 0;
private float inputX = 0;
private float inputForce = 0f;
private Transform target;
public float rakeAngle = 25f;
private float wheelBase = 0f;
public float turningRadius = 0f;
public float leanAngle = 0f;
private float m2bRatio = 0f;
private float zMass = 0f;
private Vector3 centerOfMass = new Vector3(0f,-1f,0f);
// Use this for initialization
void Start () {
target = transform;
m2bRatio = rearWheel.radius * 3.28084f * 2.20462f;
rigidbody.centerOfMass = centerOfMass;
rigidbody.mass = mass;
wheelBase = Mathf.Abs (Vector3.Distance (frontWheel.transform.localPosition, rearWheel.transform.localPosition));
}
// Update is called once per frame
void Update () {
//Getting Platform and input Value from the palyer
switch (Application.platform) {
case RuntimePlatform.IPhonePlayer:
case RuntimePlatform.Android:
inputX = -Input.acceleration.y;
break;
case RuntimePlatform.OSXPlayer:
case RuntimePlatform.OSXEditor:
case RuntimePlatform.OSXWebPlayer:
case RuntimePlatform.WindowsPlayer:
case RuntimePlatform.WindowsEditor:
case RuntimePlatform.WindowsWebPlayer:
inputY = Input.GetAxis (“Vertical”);
inputX = Input.GetAxis (“Horizontal”);
break;
}
rigidbody.drag = rigidbody.velocity.magnitude / 250;
if (inputX != 0)
turnAngle += inputX * Time.deltaTime * 02f;
//else if (turnAngle < -0.025 || turnAngle > 0.025){
// turnAngle = (-turnAngle) * Time.deltaTime *0.1f ;}
else
turnAngle = 0;
isBrake = false;
if (inputY > 0)
inputForce += acceleration;
if (inputY < 0) {
inputForce -= (inputForce * 0.10f);
isBrake = true;
}
if (inputY == 0)
inputForce -= (inputForce * 0.10f);
if (inputForce < 0)
inputForce = 0;
if (speed >= maxSpeed || engineRMP >= maxEngineRPM)
inputForce -= inputForce * 0.10f;
engineTorque = inputForce * m2bRatio;
torqueRatio = (engineTorque) / gearRatio [intCurrentGear];
if (isBrake) {
rearWheel.motorTorque = 0;
rearWheel.brakeTorque = mass;
} else {
rearWheel.brakeTorque = 0;
rearWheel.motorTorque = torqueRatio;
}
fltHandBrake = (Input.GetButton (“Jump”) ? 1.0f : 0.0f);
if (fltHandBrake > 0)
frontWheel.brakeTorque = mass;
else
frontWheel.brakeTorque = 0;
speed = (target.rigidbody.velocity.magnitude * 3.6f);
//code for un tilt bike from back
if (speed >= 1f)
zMass = (-1.55f / 228f) * speed;
else
zMass = 0f;
centerOfMass.Set (0, -1, zMass);
rigidbody.centerOfMass = centerOfMass;
// code end
engineRMP = rearWheel.rpm * gearRatio [intCurrentGear];
audio.pitch = Mathf.Abs (engineRMP / maxEngineRPM) + 1.0f;
turnAngle = Mathf.Clamp (turnAngle, -maxSteer, maxSteer);
frontWheel.steerAngle = turnAngle;
if (turnAngle != 0) {
turningRadius = wheelBase / (turnAngle * Mathf.Cos (rakeAngle));
leanAngle = Mathf.Atan (Mathf.Pow (speed, 2f) / (turningRadius));
} else
leanAngle = 0;
relativeVelocity = transform.InverseTransformDirection (rigidbody.velocity);
gyroscope = Mathf.Clamp01 ((rigidbody.velocity.magnitude) / 20);
gyrosteer = (1 - gyroscope) * 0.90f;
steerLean = turnAngle * gyroscope;
leanAngle = turnAngle - steerLean * gyrosteer;
//leanAngle = (speed / maxSpeed) * turnAngle * 100;
//leanAngle = Mathf.Clamp (leanAngle, -45, 45);
//target.eulerAngles = new Vector3 (target.localEulerAngles.x, target.localEulerAngles.y, -leanAngle);
//target.eulerAngles.Set(0,0,leanAngle);
//transform.localEulerAngles.Set (0, 0, -steerLean);
}
void FixedUpdate () {
ShiftGears ();
guiSpeed.text = “Speed: " + speed.ToString (“f2”) + " KM/h”;
guiGear.text = "Currne Gear: " + intCurrentGear.ToString () + ", Input Force: " + inputForce.ToString (“f2”);
guiRPM.text = "Engine RPM: " + engineRMP.ToString (“f2”) + ", RW RPM: " + rearWheel.rpm;
}
void ShiftGears () {
if (engineRMP >= 1400f && intCurrentGear < gearRatio.Length - 1) {
intCurrentGear ++;
inputForce = inputForce * 0.75f;
}
if (engineRMP <= 800f && intCurrentGear > 0) {
intCurrentGear --;
inputForce = inputForce * 0.75f;
}
}
}