Search Unity

trailer wheels problem.

Discussion in 'Physics' started by nbg_yalta, Sep 11, 2015.

  1. nbg_yalta

    nbg_yalta

    Joined:
    Oct 3, 2012
    Posts:
    378
    Hello, I have a simple car physics and I'm trying to make a trailer for a car. Connection done with configurable joint and all the things looks fine. the problem is trailer wheel colliders wont spin.
     
  2. MikeUpchat

    MikeUpchat

    Joined:
    Sep 24, 2010
    Posts:
    1,056
    You do need to script the rotation of the wheel mesh objects, you use the GetWorldPose() method o the wheel colliders for the trailer then use the returned rot and position values to set the trailer wheel objects transform.
     
  3. pauloaguiar

    pauloaguiar

    Joined:
    May 13, 2009
    Posts:
    700
    You can try this:
    Code (JavaScript):
    1. function UpdatesMeshesPositionErotation ()
    2. {
    3.     for (var i : int = 0; i < 4; i++) //4 mean the currect number of wheels
    4.     {
    5.         var rot : Quaternion;
    6.         var pos : Vector3;
    7.         wheelsCollider[i].GetWorldPose(pos, rot);
    8.        
    9.         tireMesh[i].position = pos;
    10.         tireMesh[i].rotation = rot;
    11.     }
    12. }
    call UpdatesMeshesPositionErotation (); in Update.
     
  4. nbg_yalta

    nbg_yalta

    Joined:
    Oct 3, 2012
    Posts:
    378
    I know about this, and my code already do this. As you can clearly see at 1:50, there is no wheel collider (not mesh) rotation... So this is not a solution... Joint connection blocking wheels somehow...
     
    Last edited: Sep 12, 2015
  5. pauloaguiar

    pauloaguiar

    Joined:
    May 13, 2009
    Posts:
    700
    Hmm that´s strange:(
    try using this if work. just delete perfabs script part or renamed. Add to the wheels.
    Code (JavaScript):
    1. // Define the variables used in the script, the Corresponding collider is the wheel collider at the position of
    2. // the visible wheel, the slip prefab is the prefab instantiated when the wheels slide, the rotation value is the
    3. // value used to rotate the wheel around it's axel.
    4. var CorrespondingCollider : WheelCollider;
    5. var SlipPrefab : GameObject;
    6. var slipSmookePower : float = 5.0;
    7. private var RotationValue : float = 0.0;
    8.  
    9. function Update () {
    10.  
    11.     // define a hit point for the raycast collision
    12.     var hit : RaycastHit;
    13.     // Find the collider's center point, you need to do this because the center of the collider might not actually be
    14.     // the real position if the transform's off.
    15.     var ColliderCenterPoint : Vector3 = CorrespondingCollider.transform.TransformPoint( CorrespondingCollider.center );
    16.    
    17.     // now cast a ray out from the wheel collider's center the distance of the suspension, if it hit something, then use the "hit"
    18.     // variable's data to find where the wheel hit, if it didn't, then se tthe wheel to be fully extended along the suspension.
    19.     if ( Physics.Raycast( ColliderCenterPoint, -CorrespondingCollider.transform.up, hit, CorrespondingCollider.suspensionDistance + CorrespondingCollider.radius ) ) {
    20.         transform.position = hit.point + (CorrespondingCollider.transform.up * CorrespondingCollider.radius);
    21.     }else{
    22.         transform.position = ColliderCenterPoint - (CorrespondingCollider.transform.up * CorrespondingCollider.suspensionDistance);
    23.     }
    24.    
    25.     // now set the wheel rotation to the rotation of the collider combined with a new rotation value. This new value
    26.     // is the rotation around the axle, and the rotation from steering input.
    27.     transform.rotation = CorrespondingCollider.transform.rotation * Quaternion.Euler( RotationValue, CorrespondingCollider.steerAngle, 0 );
    28.     // increase the rotation value by the rotation speed (in degrees per second)
    29.     RotationValue += CorrespondingCollider.rpm * ( 360/60 ) * Time.deltaTime;
    30.    
    31.     // define a wheelhit object, this stores all of the data from the wheel collider and will allow us to determine
    32.     // the slip of the tire.
    33.     var CorrespondingGroundHit : WheelHit;
    34.     CorrespondingCollider.GetGroundHit( CorrespondingGroundHit );
    35.    
    36.     // if the slip of the tire is greater than 2.0, and the slip prefab exists, create an instance of it on the ground at
    37.     // a zero rotation.
    38.     if ( Mathf.Abs( CorrespondingGroundHit.sidewaysSlip ) > slipSmookePower ) {
    39.         if ( SlipPrefab ) {
    40.             Instantiate( SlipPrefab, CorrespondingGroundHit.point, Quaternion.identity );
    41.         }      
    42.        
    43.     }  
    44.    
    45. }
     
  6. nbg_yalta

    nbg_yalta

    Joined:
    Oct 3, 2012
    Posts:
    378
    Script makes wheel colliders shaking crazy...
     
  7. nbg_yalta

    nbg_yalta

    Joined:
    Oct 3, 2012
    Posts:
    378
    Trailer wheel colliders works only if I set Y Motion of my configurable joint to Free, but it breaking connection in Y axis...
     
  8. nbg_yalta

    nbg_yalta

    Joined:
    Oct 3, 2012
    Posts:
    378
    Ok, Looks like I've found the reason - kinematic rigidbody on the car trailer holder object... Adding joint to an actual car fixing this issue. Another problem I've got now is when the trailer rigidbody mass is > 80 the car with mass 2000 can't pull it...
     
  9. pauloaguiar

    pauloaguiar

    Joined:
    May 13, 2009
    Posts:
    700
    try reduce the mass <-80 of the trailer.
     
  10. nbg_yalta

    nbg_yalta

    Joined:
    Oct 3, 2012
    Posts:
    378
    This is not a solution... too light trailer will behave wrong... Also looks like joint just holds my car.
    Car just can't move, but when I drag it a little in scene, something breaking and releasing my car, and after full stop, this infernal forces begins to hold it again :D How to deal with it?

     
    Last edited: Sep 13, 2015
  11. pauloaguiar

    pauloaguiar

    Joined:
    May 13, 2009
    Posts:
    700
    test in export game to exe and play if that behavior still there. or if you want export assent package for testing.
     
  12. nbg_yalta

    nbg_yalta

    Joined:
    Oct 3, 2012
    Posts:
    378
    Same... Have no idea why
     
  13. MacStanley

    MacStanley

    Joined:
    Jul 18, 2013
    Posts:
    118
    Has this problem already been fixed? Curious because traing things with trailers as well currently.
     
  14. nbg_yalta

    nbg_yalta

    Joined:
    Oct 3, 2012
    Posts:
    378
  15. Sanyi

    Sanyi

    Joined:
    Aug 22, 2015
    Posts:
    1
    Hi,

    I have the same problem with trailer.
    I add a small amount of torque (10Nm) to trailer wheelcolliders, until it's starts to move.
     
    SimonCVintecc likes this.
  16. XSpitFire

    XSpitFire

    Joined:
    Jan 22, 2018
    Posts:
    15
    Anybody figured this out ? As soon as the trailer weight is set to anything above 50 it will not move. The truck just spins its wheels, but doesnt move at all
     
  17. the_mr_matt

    the_mr_matt

    Joined:
    Jul 21, 2015
    Posts:
    124
    Sorry to bump a 4 year old thread but I can't find the same issue anywhere else. Did anyone here find a proper solution?

    Come on unity. It's 2019 already, get your act together. The wheelcollider is just awful.
     
  18. nbg_yalta

    nbg_yalta

    Joined:
    Oct 3, 2012
    Posts:
    378
    This may not be a solution, but try to set rigidbody.sleepThreshold to 0 or sleeping mode to Never... Let me know if it works
     
    Last edited: Apr 12, 2021
  19. PavelLangweil

    PavelLangweil

    Joined:
    Mar 4, 2014
    Posts:
    28
    I'm just beginning with this in Unity, so it's likely we're all missing something very basic. The workaround I found so far (and also mentioned here https://www.winglett.com/making-a-car-trailer/), is creating a "fake motor" on the trailer. It does work for me, gets the whole thing moving without affecting anything really. It is weird though for sure, that's not how physics supposed to work, so I'm still thinking we're missing something.
     
  20. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,442
    Thanks for your note. Yes, even a 0.00001f motorTorque on each update will allow the WheelCollider to "break loose" and be considered dynamic by the physics engine. Otherwise the physics engine is trying too hard to NOT consider things that should be stopped at rest, so they don't go rolling off on their own, and the physics engine does not see the trailer hinge as causing a torque on the axles.

    That said, I also found that an Edy's Vehicle Controller will correctly handle the trailer, including all the tire-slipping traction, which a WheelCollider will not do properly. Just give it the right number of wheels, specify that none of the trailer wheels are "drive" tires, and include a Configurable Joint (every axis locked but X and Y Rotation).
     
    PavelLangweil likes this.