Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

WheelColliders and rotation

Discussion in 'Editor & General Support' started by Erikoinen, Dec 31, 2015.

  1. Erikoinen

    Erikoinen

    Joined:
    Nov 10, 2014
    Posts:
    68
    Hey all, I've made a fully functional car rig using the WheelColliders and I'm ALMOST happy with it, but there is a problem with how the WheelColliders behave. I've read a bunch of similar issues about rotating WheelColliders, and know that you shouldn't do it - and I'm not doing it on purpose, but the car root object rotates and therefore the wheels do too. The wheels cannot be rotated by hand (at least the visual collider doesn't change) so I can't negate the rotation by code.

    Here's the issue with images:

    1. The setup. I removed all other colliders and particles except the wheels so it's easier to understand.
    upload_2015-12-31_13-39-35.png

    2. Rotating the Wheels gameobject results in this (correct behaviour I think, Wheel's local rotation is kept untouched)
    upload_2015-12-31_13-40-39.png

    3. Rotating the Car node (with the Rigidbody) however translates into this. You can see the wheel colliders actually rotating, even though I believe they shouldn't:
    upload_2015-12-31_13-41-21.png


    And in gameplay it ends up making the wheels sink into the ground, and if rotated too much, funky behaviour happens (becomes very jumpy etc. looking like collision bugs) and they start losing traction.


    I don't know to fix this issue. Any help very welcome!
     
  2. neekoh

    neekoh

    Joined:
    Nov 16, 2012
    Posts:
    9
    Moi!
    While I don't have recent first-hand knowledge of Unity 5 wheel colliders, I've played a bit with them and still think some general rules from Unity 4 apply. I hope you find some clue to your problem.
    - The wheel colliders should be placed under the car rigidbody in hierarchy as you've done, they will affect the car rigidbody directly. No extra rigidbodies per wheel. Actually extra rigidbodies per car (considerably affecting the system's mass) may not work correctly.
    - As the car rigidbody rotates the wheel colliders are allowed to and will rotate. You shouldn't have to do anything about it. The situation in 3. looks like it should be.
    - The wheels are not really wheels, they are rays casted from the wheel center + radius down, towards the car rigidbody's negative y axis. In Unity 5 I think they are oriented like this regardless of wheel collider transformation. On level ground the suspension force will be applied to the car rigidbody in its y axis, and the friction forces in its x and z axes.
    - As they are rays, the wheel will "sink" into objects approaching the wheel from the front or back. The circular representation of the wheel collider gizmo may lead you to think otherwise.
    - If the car leans on its side too much, the wheel colliders seem to lose their colliding ability.
    - Unity 5 seems to be quite sensitive to car mass and the suspension parameters. Start with values found in a working example.
     
  3. Erikoinen

    Erikoinen

    Joined:
    Nov 10, 2014
    Posts:
    68
    Hey neekoh, as I said the car is otherwise behaving very nicely, including its springs etc. I found those values exactly by working down from an existing example and reducing the example code base (~2000 lines) to about ~150, to the parts I need.

    Your third point there: That's what I've understood, that they raycast to negative y, but they seem to do it in their local space instead of world space. Because they do it in the local space, the collision happens in a non-wanted way. See below image for what I'm after:
    upload_2015-12-31_14-40-3.png
     
  4. Erikoinen

    Erikoinen

    Joined:
    Nov 10, 2014
    Posts:
    68
    Of course the dumb in me didn't take into account what then would happen if the car goes uphill, i.e. the "Not wanted behaviour" would actually be the wanted behavior then.

    So the answer to this is, the WheelCollider is not meant to be used with cars/motorcycles that can make wheelies and such?
     
  5. neekoh

    neekoh

    Joined:
    Nov 16, 2012
    Posts:
    9
    I'm afraid that's right. The wheels work in their local space (~ rigidbody's local space). For proper wheelies and ability to climb walls you need to come up with something else, and I don't think an out-of-the-box solution exists. AssetStore may prove me wrong, as always.
    Just throwing one possibility in the air for at least for wheelies, but it's a hack and leaves a lot to implement for you.
    Use a rigidbody with a sphere collider per wheel and fix the wheels to the car body with spring joints. Make the wheels completely frictionless, and calculate the friction / driving forces yourself. Each wheel's offset from the rigged position will give you the load acting on the tyre (based on F = kx), with which you can scale the friction forces that you apply to the car rigidbody at the wheel position (or contact position if you like). Physically the wheel spheres will be moving in all directions when hitting obstacles but you can place the visuals along the y axis. We got away with a "cheap" solution like this for something convincing enough (the cop cars in Turbo Dismount ;) ).
     
  6. Erikoinen

    Erikoinen

    Joined:
    Nov 10, 2014
    Posts:
    68
    I started off by playing around with spheres and spring joints, but couldn't figure out how to limit the spring joint's behaviour to one axis (since it moves to all directions) and therefore got myself familiar with the WheelCollider. I'm afraid I might not be a competent programmer enough to create the whole rig by myself.

    Currently, with the WheelColliders, even with the "bug", the behaviour is okayish, and the "bug" is only recognizable in extreme cases. I've done a few collider-based workarounds to hide the bug the best I can. So I might just go with this for now.

    Thanks for the help, neekoh!
     
  7. Erikoinen

    Erikoinen

    Joined:
    Nov 10, 2014
    Posts:
    68
    Btw if you have an idea on limiting the sprint joint's movement, I'll be happy to hear 'em. :)
     
  8. bougimon

    bougimon

    Joined:
    Dec 6, 2013
    Posts:
    2
    In which way are you rotating the car node?

    These methods came to my mind first, tested them just briefly and they seem to produce somewhat wanted behaviour:

    1)
    GetComponent.<Rigidbody>().AddTorque(transform.right * -forceVariable, ForceMode.Impulse);

    2)
    GetComponent.<Rigidbody>().AddForceAtPosition(Vector3.up * forceVariable, carBumperPosition, ForceMode.Impulse);
     
  9. Erikoinen

    Erikoinen

    Joined:
    Nov 10, 2014
    Posts:
    68
    The problem exists even if I don't manually rotate the car at all, since the physics make the car rotate as it goes over bumps and hills.

    But to answer your question, I use AddTorque when the car is not touching the ground, but that's more of a gameplay thing.