- Home /
Motorcycle Physic Move bikes up from back side
Hi Friends,
I am New bee in Unity3D Development. Now days i am working on Bike Racing Game, and dealing with Physic and WheelCollider.
I am in my wheels and bike body with a Empty gameObject and put another empty gameObject for colliers in main parent object for wheel collider, collider are in separate from Wheel Mesh Object.
Bike moves with applying torque on back wheel collider and a script rotated backwheel on X axis, and using physic bike moves. after such a speed Bike moves with jerks and then it's will become up on front wheel just like stunt
here I shared my script which moves bike (not which rotated wheel)
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);
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);
}
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;
}
}
}
After some web searching i found the solution but the solution is creating another problem in leaning of bike when i rotated my front wheel.
solution is i put few line of code in Update function after calculating speed on base of object Velocity magnitude which moves centerOfMass back side of bike as speed increasing
//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
So code I adds in my script is not a real solution of the problem... looking for some help...
thanks guys in andvance
Your answer
![](https://koobas.hobune.stream/wayback/20220613075341im_/https://answers.unity.com/themes/thub/images/avi.jpg)