Search Unity

Motorcycle Physic Move bikes up from back side

Discussion in 'Editor & General Support' started by PAHeartBeat, Aug 8, 2012.

  1. PAHeartBeat

    PAHeartBeat

    Joined:
    Jul 11, 2012
    Posts:
    76
    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 back-wheel 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)
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class BikeMove : MonoBehaviour {
    6.    
    7.     public const float PI  = 3.1415f;
    8.  
    9.     public WheelCollider frontWheel;
    10.     public WheelCollider rearWheel;
    11.    
    12.     public float[] gearRatio = {4.31f, 3.11f, 1.88f, 1.41f, 1.13f, 1.0f};
    13.     private int intCurrentGear = 0;
    14.     private float torqueRatio = 0f;
    15.     private bool isBrake = false;
    16.    
    17.     private float fltHandBrake = 1.0f;
    18.    
    19.     private float engineTorque = 0.0f;
    20.    
    21.     public float maxEngineRPM = 3000f;
    22.     private float engineRMP = 0f;
    23.    
    24.     //private float bikeHP = 0f;
    25.     public int acceleration =1;    
    26.        
    27.     public GUIText guiSpeed;
    28.     public GUIText guiGear;
    29.     public GUIText guiRPM;
    30.    
    31.     private float speed = 0f;
    32.     public float maxSpeed = 140;
    33.    
    34.     public float mass = 250.0f;
    35.        
    36.     public int maxSteer = 18;
    37.  
    38.     public float turnAngle = 0F;
    39.     public float gyroscope = 0.0f;
    40.     public float gyrosteer = 0.0f;
    41.     public float steerLean = 0.0f;
    42.     public float  leanFactor = 0.0f;
    43.     private Vector3 relativeVelocity = Vector3.zero;
    44.    
    45.     private float inputY = 0;
    46.     private float inputX = 0;
    47.    
    48.     private float inputForce = 0f;
    49.  
    50.     private Transform target;
    51.    
    52.     public float rakeAngle = 25f;
    53.    
    54.     private float wheelBase = 0f;
    55.     public float turningRadius = 0f;
    56.     public float leanAngle = 0f;
    57.    
    58.  
    59.     private float m2bRatio = 0f;
    60.     private float zMass = 0f;
    61.     private Vector3 centerOfMass = new Vector3(0f,-1f,0f);
    62.    
    63.     // Use this for initialization
    64.     void Start () {
    65.         target = transform;
    66.         m2bRatio = rearWheel.radius * 3.28084f * 2.20462f;
    67.         rigidbody.centerOfMass = centerOfMass;
    68.         rigidbody.mass = mass;
    69.         wheelBase = Mathf.Abs (Vector3.Distance (frontWheel.transform.localPosition, rearWheel.transform.localPosition));
    70.     }
    71.    
    72.     // Update is called once per frame
    73.     void Update () {
    74.        
    75.         //Getting Platform and input Value from the palyer
    76.         switch (Application.platform) {
    77.         case RuntimePlatform.IPhonePlayer:
    78.         case RuntimePlatform.Android:
    79.             inputX = -Input.acceleration.y;
    80.             break;
    81.  
    82.         case RuntimePlatform.OSXPlayer:
    83.         case RuntimePlatform.OSXEditor:
    84.         case RuntimePlatform.OSXWebPlayer:
    85.         case RuntimePlatform.WindowsPlayer:
    86.         case RuntimePlatform.WindowsEditor:
    87.         case RuntimePlatform.WindowsWebPlayer:
    88.             inputY = Input.GetAxis ("Vertical");
    89.             inputX = Input.GetAxis ("Horizontal");
    90.             break;
    91.         }
    92.        
    93.         rigidbody.drag = rigidbody.velocity.magnitude / 250;
    94.         if (inputX != 0)
    95.             turnAngle += inputX * Time.deltaTime * 02f;
    96.         //else if (turnAngle < -0.025 || turnAngle > 0.025){
    97.         //    turnAngle = (-turnAngle) * Time.deltaTime *0.1f ;}
    98.         else
    99.             turnAngle = 0;
    100.        
    101.         isBrake = false;
    102.         if (inputY > 0)
    103.             inputForce += acceleration;
    104.        
    105.         if (inputY < 0) {
    106.             inputForce -= (inputForce * 0.10f);
    107.             isBrake = true;
    108.         }
    109.         if (inputY == 0)
    110.             inputForce -= (inputForce * 0.10f);
    111.        
    112.         if (inputForce < 0)
    113.             inputForce = 0;
    114.        
    115.         if (speed >= maxSpeed || engineRMP >= maxEngineRPM)
    116.             inputForce -= inputForce * 0.10f;
    117.        
    118.         engineTorque = inputForce * m2bRatio;
    119.         torqueRatio = (engineTorque) / gearRatio [intCurrentGear];
    120.    
    121.         if (isBrake) {
    122.             rearWheel.motorTorque = 0;
    123.             rearWheel.brakeTorque = mass;
    124.         } else {
    125.             rearWheel.brakeTorque = 0;
    126.             rearWheel.motorTorque = torqueRatio;
    127.         }
    128.  
    129.         fltHandBrake = (Input.GetButton ("Jump") ? 1.0f : 0.0f);
    130.         if (fltHandBrake > 0)
    131.             frontWheel.brakeTorque = mass;
    132.         else
    133.             frontWheel.brakeTorque = 0;
    134.        
    135.         speed = (target.rigidbody.velocity.magnitude * 3.6f);
    136.        
    137.         engineRMP = rearWheel.rpm * gearRatio [intCurrentGear];
    138.        
    139.         audio.pitch = Mathf.Abs (engineRMP / maxEngineRPM) + 1.0f;
    140.  
    141.         turnAngle = Mathf.Clamp (turnAngle, -maxSteer, maxSteer);    
    142.         frontWheel.steerAngle = turnAngle;
    143.        
    144.         if (turnAngle != 0) {
    145.             turningRadius = wheelBase / (turnAngle * Mathf.Cos (rakeAngle));
    146.             leanAngle = Mathf.Atan (Mathf.Pow (speed, 2f) / (turningRadius));
    147.         } else
    148.             leanAngle = 0;
    149.        
    150.        
    151.         target.eulerAngles.Set(0,0,leanAngle);
    152.     }
    153.  
    154.    void FixedUpdate () {
    155.         ShiftGears ();        
    156.  
    157.         guiSpeed.text = "Speed: " + speed.ToString ("f2") + " KM/h";
    158.         guiGear.text = "Currne Gear: " + intCurrentGear.ToString () + ", Input Force: " + inputForce.ToString ("f2");
    159.         guiRPM.text = "Engine RPM: " + engineRMP.ToString ("f2") + ", RW RPM: " + rearWheel.rpm;
    160.     }
    161.    
    162.     void ShiftGears () {
    163.         if (engineRMP >= 1400f  intCurrentGear < gearRatio.Length - 1) {
    164.             intCurrentGear ++;
    165.             inputForce = inputForce * 0.75f;
    166.         }
    167.    
    168.         if (engineRMP <= 800f  intCurrentGear > 0) {
    169.             intCurrentGear --;
    170.             inputForce = inputForce * 0.75f;
    171.         }
    172.     }
    173. }
    174.  
    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 (csharp):
    1.  
    2.         //code for un tilt bike from back
    3.         /*if (speed >= 1f)
    4.             zMass = (-1.55f / 228f) * speed;
    5.         else
    6.             zMass = 0f;
    7.        
    8.         centerOfMass.Set (0, -1, zMass);
    9.         rigidbody.centerOfMass = centerOfMass;*/
    10.         // code end
    11.  
    So code I adds in my script is not a real solution of the problem... looking for some help...
    thanks guys in advance
     
    Last edited: Aug 8, 2012
  2. wana7262

    wana7262

    Joined:
    Jul 2, 2012
    Posts:
    103
    Hi did u completed the Bike physics? can u share the scripts? or a package? thanks....!!!!
     
  3. LuizNegrini

    LuizNegrini

    Joined:
    Sep 28, 2013
    Posts:
    3
    I need this, please!