Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Loading Vehicle From Files - Flipping Problem

Discussion in 'Scripting' started by ZO5KmUG6R, Sep 30, 2014.

  1. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    I know some people are experienced with this. So I need some help

    Here is my code. The problem is when the car is loaded, it flips at about 10mph.

    Look at the part where I commented "//This messes it up =c". Without that it doesnt flip until 40mph. Which it still shouldnt flip right over at.


    Code (CSharp):
    1.  
    2. public static GameObject LoadVehicle(string vehicleName,bool ai = false){
    3.  
    4.      //Prepare base variables
    5.      string CarName = vehicleName;
    6.      string CarFile = dataPath + "/vehicles/" + vehicleName;
    7.      
    8.      //Load in our 3D Models
    9.      GameObject car = new GameObject(CarName);
    10.      GameObject carModel = tmdl_load.LoadTML (CarFile + "/" + CarName + ".tml",false,true,"Reflective/Diffuse",true);
    11.      GameObject wheelModel = tmdl_load.LoadTML (CarFile + "/wheel.tml");
    12.      
    13.      //Add rigidbody and setup
    14.      car.AddComponent<Rigidbody>();
    15.      Rigidbody r = car.GetComponent<Rigidbody>();
    16.      r.interpolation = RigidbodyInterpolation.Interpolate;
    17.      r.collisionDetectionMode = CollisionDetectionMode.Continuous;
    18.      r.Sleep ();
    19.      car.SetActive (false);
    20.      
    21.      //Add a collider
    22.      car.AddComponent<MeshCollider>();
    23.      MeshCollider CCgb = car.GetComponent<MeshCollider>();
    24.      carModel.transform.parent = car.transform;
    25.      wheelModel.transform.parent = car.transform;
    26.      Vector3 com = Vector3.zero;
    27.      carModel.transform.position = Vector3FromString ("0,0,0");
    28.        
    29.      GameObject wh_fl = new GameObject("WheelHubFL");
    30.      GameObject wh_fr = new GameObject("WheelHubFR");
    31.      GameObject wh_bl = new GameObject("WheelHubBL");
    32.      GameObject wh_br = new GameObject("WheelHubBR");
    33.      
    34.      wh_fl.transform.parent = car.transform;
    35.      wh_fr.transform.parent = car.transform;
    36.      wh_bl.transform.parent = car.transform;
    37.      wh_br.transform.parent = car.transform;
    38.  
    39.      wh_fl.AddComponent<WheelCollider>();
    40.      wh_fr.AddComponent<WheelCollider>();
    41.      wh_bl.AddComponent<WheelCollider>();
    42.      wh_br.AddComponent<WheelCollider>();
    43.  
    44.      wh_fl.AddComponent<MCRDParticleEmitter>();
    45.      wh_fl.GetComponent<MCRDParticleEmitter>().LoadParticleConfigFile (Globals.dataPath + "/particles/tiresmoke.particle");
    46.  
    47.      wh_fr.AddComponent<MCRDParticleEmitter>();
    48.      wh_fr.GetComponent<MCRDParticleEmitter>().LoadParticleConfigFile (Globals.dataPath + "/particles/tiresmoke.particle");
    49.      wh_bl.AddComponent<MCRDParticleEmitter>();
    50.      wh_bl.GetComponent<MCRDParticleEmitter>().LoadParticleConfigFile (Globals.dataPath + "/particles/tiresmoke.particle");
    51.      wh_br.AddComponent<MCRDParticleEmitter>();
    52.      wh_br.GetComponent<MCRDParticleEmitter>().LoadParticleConfigFile (Globals.dataPath + "/particles/tiresmoke.particle");
    53.  
    54.      GameObject wheel2 = (GameObject)Instantiate (wheelModel);
    55.      GameObject wheel3 = (GameObject)Instantiate (wheelModel);
    56.      GameObject wheel4 = (GameObject)Instantiate (wheelModel);
    57.  
    58.      wheel2.transform.localScale = new Vector3(-1,1,-1);
    59.      wheel4.transform.localScale = new Vector3(-1,1,-1);
    60.  
    61.      wheel2.transform.parent = car.transform;
    62.      wheel3.transform.parent = car.transform;
    63.      wheel4.transform.parent = car.transform;
    64.      
    65.      List<Wheel> wheelList = new List<Wheel>();
    66.      CarController cc;
    67.      car.AddComponent<CarController>();
    68.      cc = car.GetComponent<CarController>();
    69.      cc.advanced = new CarController.Advanced();
    70.      
    71.      INIFile.SetReadPath (CarFile + "/" + CarName + ".veh");
    72.      bool addAntiRoll = Convert.ToBoolean (INIFile.IniReadValue ("Parameters","antiroll"));
    73.      if(addAntiRoll){
    74.        car.AddComponent<AntiRollBar>();
    75.        car.AddComponent<AntiRollBar>();
    76.  
    77.        AntiRollBar[] rollBars =  car.GetComponents<AntiRollBar>();
    78.  
    79.        float FrontRoll = Convert.ToSingle (INIFile.IniReadValue ("Parameters","antiroll_front"));
    80.        float RearRoll = Convert.ToSingle (INIFile.IniReadValue ("Parameters","antiroll_rear"));
    81.  
    82.        rollBars[0].antiRollVal = FrontRoll;
    83.        rollBars[0].wheelL = wh_fl.GetComponent<WheelCollider>();
    84.        rollBars[0].wheelR = wh_fr.GetComponent<WheelCollider>();
    85.  
    86.        
    87.        rollBars[1].antiRollVal = RearRoll;
    88.        rollBars[1].wheelL = wh_bl.GetComponent<WheelCollider>();
    89.        rollBars[1].wheelR = wh_br.GetComponent<WheelCollider>();
    90.        
    91.      }
    92.  
    93.      r.mass  = Convert.ToSingle (INIFile.IniReadValue ("Parameters","mass"));
    94.      r.drag  = Convert.ToSingle (INIFile.IniReadValue ("Parameters","drag"));
    95.      r.angularDrag = Convert.ToSingle (INIFile.IniReadValue ("Parameters","angular_drag"));
    96.  
    97.      //Load collider mesh
    98.      Mesh carColliderMSH = tmdl_load.LoadTML (CarFile + "/" + CarName + "_COLLISION.tml",true,false).GetComponent<MeshCollider>().sharedMesh;
    99.  
    100.      CCgb.convex = true;
    101.      CCgb.sharedMesh = carColliderMSH; //This messes it up =c
    102.  
    103.  
    104.  
    105.    
    106.  
    107.      cc.steeringResponseSpeed = Convert.ToSingle (INIFile.IniReadValue ("Parameters","steer_response_speed"));
    108.      cc.maxSteerAngle = Convert.ToSingle (INIFile.IniReadValue ("Parameters","max_steer_angle"));
    109.      cc.maxSpeedSteerAngle =  Convert.ToSingle (INIFile.IniReadValue ("Parameters","max_speed_steer_angle"));
    110.      cc.maxSpeedSteerResponse =  Convert.ToSingle (INIFile.IniReadValue ("Parameters","max_speed_steer_response"));
    111.      cc.maxSpeed =  Convert.ToSingle (INIFile.IniReadValue ("Parameters","max_speed"));
    112.      cc.maxTorque =  Convert.ToSingle (INIFile.IniReadValue ("Parameters","max_torque"));
    113.      cc.minTorque =  Convert.ToSingle (INIFile.IniReadValue ("Parameters","min_torque"));
    114.      cc.brakePower =  Convert.ToSingle (INIFile.IniReadValue ("Parameters","brake_power"));
    115.      com = Vector3FromStringReal (INIFile.IniReadValue ("Parameters","com_adjust"));
    116.      //Advanced Parameters
    117.      cc.advanced.burnoutSlipEffect =  Convert.ToSingle (INIFile.IniReadValue ("Parameters","burnout_slip_effect"));
    118.      cc.advanced.burnoutTendency =  Convert.ToSingle (INIFile.IniReadValue ("Parameters","burnout_tendency"));
    119.      cc.advanced.spinoutSlipEffect =  Convert.ToSingle (INIFile.IniReadValue ("Parameters","spinout_slip_effect"));
    120.      cc.advanced.revRangeBoundary =  Convert.ToSingle (INIFile.IniReadValue ("Parameters","rev_range_boundary"));
    121.      cc.advanced.numGears= Convert.ToInt32 (INIFile.IniReadValue ("Parameters","num_gears"));
    122.      cc.advanced.downForce = Convert.ToSingle (INIFile.IniReadValue ("Parameters","downforce"));
    123.      cc.advanced.sideSlideEffect = Convert.ToSingle (INIFile.IniReadValue ("Parameters","side_slide_effect"));
    124.      cc.advanced.gearDistributionBias = Convert.ToSingle (INIFile.IniReadValue ("Parameters","gear_distribution_bias"));
    125.      cc.advanced.steeringCorrection = Convert.ToSingle (INIFile.IniReadValue ("Parameters","steering_correction"));
    126.      cc.advanced.oppositeLockSteeringCorrection = Convert.ToSingle (INIFile.IniReadValue ("Parameters","opposite_lock_steering_correction"));
    127.      cc.advanced.reversingSpeedFactor = Convert.ToSingle (INIFile.IniReadValue ("Parameters","reversing_speed_factor"));
    128.      cc.advanced.skidGearLockFactor = Convert.ToSingle (INIFile.IniReadValue ("Parameters","skid_gear_lock_factor"));
    129.      cc.advanced.accelChangeSmoothing = Convert.ToSingle (INIFile.IniReadValue ("Parameters","accel_change_smoothing"));
    130.      cc.advanced.gearFactorSmoothing = Convert.ToSingle (INIFile.IniReadValue ("Parameters","gear_factor_smoothing"));
    131.  
    132.    
    133.      cc.preserveDirectionWhileInAir = true;
    134.  
    135.  
    136.      
    137.      wh_fl.transform.position = Vector3FromString (INIFile.IniReadValue ("Wheels","fl_wheelhub"));
    138.      wheelModel.transform.position = wh_fl.transform.position;
    139.  
    140.  
    141.      
    142.      wh_fr.transform.position = Vector3FromString (INIFile.IniReadValue ("Wheels","fr_wheelhub"));
    143.      wheel2.transform.position = wh_fr.transform.position;
    144.  
    145.      wh_br.transform.position = Vector3FromString (INIFile.IniReadValue ("Wheels","br_wheelhub"));
    146.      wheel4.transform.position = wh_br.transform.position;
    147.  
    148.      wh_bl.transform.position = Vector3FromString (INIFile.IniReadValue ("Wheels","bl_wheelhub"));
    149.      wheel3.transform.position = wh_bl.transform.position;
    150.  
    151.  
    152.      string line =  INIFile.IniReadValue ("Wheels","fl_parameters");
    153.      WheelCollider wc = wh_fl.GetComponent<WheelCollider>();
    154.      WheelColliderFromString (line,ref wc);
    155.  
    156.      wh_fl.AddComponent<Wheel>();
    157.      wh_fl.GetComponent<Wheel>().powered = Convert.ToBoolean (line.Split(',')[20]);
    158.      wh_fl.GetComponent<Wheel>().steerable = Convert.ToBoolean (line.Split(',')[19]);
    159.      wh_fl.GetComponent<Wheel>().slideThreshold = Convert.ToSingle (line.Split(',')[22]);
    160.      wh_fl.GetComponent<Wheel>().particleRate = Convert.ToSingle (line.Split(',')[21]);
    161.      wh_fl.GetComponent<Wheel>().wheelModel = wheelModel.transform;
    162.      WheelColliderFromString2 (line,ref wc);
    163.      wh_fl.GetComponent<Wheel>().StartPublic ();
    164.  
    165.      wheelList.Add (wh_fl.GetComponent<Wheel>());
    166.  
    167.      line = INIFile.IniReadValue ("Wheels","fr_parameters");
    168.      wc = wh_fr.GetComponent<WheelCollider>();
    169.      WheelColliderFromString (line,ref wc);
    170.  
    171.      wh_fr.AddComponent<Wheel>();
    172.      wh_fr.GetComponent<Wheel>().powered = Convert.ToBoolean (line.Split(',')[20]);
    173.      wh_fr.GetComponent<Wheel>().steerable = Convert.ToBoolean (line.Split(',')[19]);
    174.      wh_fr.GetComponent<Wheel>().slideThreshold = Convert.ToSingle (line.Split(',')[22]);
    175.      wh_fr.GetComponent<Wheel>().particleRate = Convert.ToSingle (line.Split(',')[21]);
    176.      wh_fr.GetComponent<Wheel>().wheelModel = wheel2.transform;
    177.      WheelColliderFromString2 (line,ref wc);
    178.      wh_fr.GetComponent<Wheel>().StartPublic ();
    179.  
    180.      wheelList.Add (wh_fr.GetComponent<Wheel>());
    181.  
    182.  
    183.      line = INIFile.IniReadValue ("Wheels","bl_parameters");
    184.       wc = wh_bl.GetComponent<WheelCollider>();
    185.      WheelColliderFromString (line,ref wc);
    186.  
    187.      wh_bl.AddComponent<Wheel>();
    188.      wh_bl.GetComponent<Wheel>().powered = Convert.ToBoolean (line.Split(',')[20]);
    189.      wh_bl.GetComponent<Wheel>().steerable = Convert.ToBoolean (line.Split(',')[19]);
    190.      wh_bl.GetComponent<Wheel>().slideThreshold = Convert.ToSingle (line.Split(',')[22]);
    191.      wh_bl.GetComponent<Wheel>().particleRate = Convert.ToSingle (line.Split(',')[21]);
    192.      wh_bl.GetComponent<Wheel>().wheelModel = wheel3.transform;
    193.      WheelColliderFromString2 (line,ref wc);
    194.      wh_bl.GetComponent<Wheel>().StartPublic ();
    195.  
    196.      wheelList.Add (wh_bl.GetComponent<Wheel>());
    197.  
    198.  
    199.      line = INIFile.IniReadValue ("Wheels","br_parameters");
    200.      wc = wh_br.GetComponent<WheelCollider>();
    201.      WheelColliderFromString (line,ref wc);
    202.  
    203.      wh_br.AddComponent<Wheel>();
    204.      wh_br.GetComponent<Wheel>().powered = Convert.ToBoolean (line.Split(',')[20]);
    205.      wh_br.GetComponent<Wheel>().steerable = Convert.ToBoolean (line.Split(',')[19]);
    206.      wh_br.GetComponent<Wheel>().slideThreshold = Convert.ToSingle (line.Split(',')[22]);
    207.      wh_br.GetComponent<Wheel>().particleRate = Convert.ToSingle (line.Split(',')[21]);
    208.      wh_br.GetComponent<Wheel>().wheelModel = wheel4.transform;
    209.      WheelColliderFromString2 (line,ref wc);
    210.      wh_br.GetComponent<Wheel>().StartPublic ();
    211.  
    212.      wheelList.Add (wh_br.GetComponent<Wheel>());
    213.  
    214.  
    215.  
    216.  
    217.      cc.wheels = wheelList.ToArray ();
    218.      car.AddComponent<CarUserControl>();
    219.  
    220.      
    221.      
    222.      
    223.      
    224.      
    225.  
    226.      
    227.      
    228.      car.AddComponent<AudioSource>();
    229.      car.GetComponent<AudioSource>().clip = LoadAudioFile (CarFile + "/" + INIFile.IniReadValue ("Sound","engine"));
    230.      car.GetComponent<AudioSource>().Play();
    231.      car.GetComponent<AudioSource>().loop=true;
    232.      car.GetComponent<AudioSource>().maxDistance = 50;
    233.      car.GetComponent<AudioSource>().minDistance = 25;
    234.      car.AddComponent<MicroCarAudio>();
    235.      car.GetComponent<MicroCarAudio>().source = car.GetComponent<AudioSource>();
    236.      car.GetComponent<MicroCarAudio>().controller = cc;
    237.      GameObject skidSource = new GameObject("Skid Source",typeof(AudioSource));
    238.      skidSource.transform.parent = car.transform;
    239.      skidSource.GetComponent<AudioSource>().clip = LoadAudioFile (dataPath + "/audio/skid.wav");
    240.      skidSource.GetComponent<AudioSource>().Play();
    241.      skidSource.GetComponent<AudioSource>().loop=true;
    242.      skidSource.GetComponent<AudioSource>().maxDistance = 60;
    243.      skidSource.GetComponent<AudioSource>().minDistance = 35;
    244.  
    245.      car.GetComponent<MicroCarAudio>().skidSource =    skidSource.GetComponent<AudioSource>();
    246.  
    247.      car.SetActive (true);
    248.   r.centerOfMass = com;
    249.      cc.AwakePublic ();
    250.      car.SetActive (true);
    251.      r.WakeUp ();
    252.      return car;
    253.    }
    254.  
     
  2. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,608
    I'm going to bump this.
     
  3. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    the problem is its most likely resetting the center of mass when it gets activated.

    put this code in the vehicles FixedUpdate

    if(rigidbody.centerOfMass.y > 0)
    rigidbody.centerOfMass = Vector3.zero;
     
  4. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    I'm desperate for a fix. @JamesLeeNZ, I have tried yours without any luck


    The origin of the car also isn't incorrect



    If it helps. It seems to act like a brick.

    What could be wrong!! It has been months of trial and error without any sort of fix.
     
    Last edited: Sep 30, 2014
  5. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    With a rigidbody mass of 5 flipping doesnt happen anymore :)
     
  6. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    You can't be serious!!! I'm using the tutorial settings and now im flipping at about 8mph
     
  7. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,608
    What tutorial?
     
  8. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    Standard assets * Sorry
     
  9. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    Did you put any code in to stop the rolling?

    What do you mean the origin of the car is incorrect?

    The problem has always been the center of mass being wrong from experience... perhaps put some code in to draw the location of the centerofmass.
     
  10. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    I said isn't incorrect. And there is an antiroll script as well as suspension and it still flips. :|
     
  11. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    ok, misread that part. not sure what relevance it has to anything though?

    remove/disable the anti-rolling code, which I assume you put in to try and 'fix' the car flipping during turns. It obviously isnt working correctly at any rate based on that 3rd gif.
     
  12. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    It acts the same way with it disabled. To be honest here I have never once had any success whatsoever setting up vehicles in Unity. Or anything physics related for that matter. I've had better success at Truevision 3D as a first timer to that engine, and it doesnt even have an editor.

    Bleh, Unity .-.
     
  13. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    show us your car script... theres too much unknown here. I found getting wheel colliders to work relatively easy once I got past the initial setup problems (parenting of wheel collider and setting centerofmass).

    The wheel colliders are a bit meh though. Be nice if they integrated suspension and had some decent documentation.
     
  14. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    The car script is the one from Sample Assets.