Search Unity

Any free examples showing 2D car physics?

Discussion in '2D' started by shopguy, Oct 29, 2014.

  1. shopguy

    shopguy

    Joined:
    Jan 12, 2013
    Posts:
    282
    I'm having a lot of issues using wheel joints:

    http://docs.unity3d.com/ScriptReference/WheelJoint2D.html

    The couple community discussions I've found are just "check out my asset on store" type answers... not really helpful for those that want to learn or have the source code to modify if needed.

    I'm trying to do something like "Hill Climb Racing". I figured out the basics from http://forum.unity3d.com/threads/wheel-joint-2d-motor.272912/ the video answer... but my car moves too slow and increasing the motor speed just causes it to flip, even though I'm making front and back wheels the same speed.

    When I learned the 3D wheel system, I don't remember it being such a pain. Maybe the new 2D system just isn't fully baked or I should be using a spring and hinge joint instead?
     
  2. shopguy

    shopguy

    Joined:
    Jan 12, 2013
    Posts:
    282
    I guess I was having bad luck yesterday when I tested last. I did some more "playing around" after posting this and am starting to find the right combinations. I suppose the reason nobody just says "do it this way" is maybe it does require a lot of toying with, and probably depends on the scale of your model, wheel size to body size, wheel base, height of vehicle, and lots of other factors.

    In my case I the main issue was I didn't understand "angular drag" and had set it to 0 at one point, on the car rigidbody, because I was thinking "less drag = faster", but of course angular drag of 0 just made it have 0 friction against rotation (aka-flipping over).
     
  3. CalaveraX

    CalaveraX

    Joined:
    Jul 11, 2013
    Posts:
    143
    Maybe you can check by assigning some physics material to the floor and wheels, and play arround with the friction values.

    I had some troubles also with getting a good feeling of speed with the car, and all the physics broken flying arround when passing a certain amount of motor torque.
     
  4. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,521
    It requires a whole lot of customization. The only thing the Wheel Joint2D is good for out of the box is constraining the rigidbody to a particular axis when it articulates.

    You can assign multiple Joints to any single rigidbody so take advantage of that. For instance, you can use a Hinge Joint and Wheel collider on the same object which gives you some more control over position and suspension, and further control it with a Spring joint. The biggest problem is that there are no controls for hard suspension distances so anything you create is inevitably floppy on a frustrating level.

    I haven't tried any of the asset store stuff but I can confirm it's a royal PITA to do anything practical with the 2D physics system. It's basically Box2D but the names of all the joints are different and they've implemented it in a sort of strange way. It's like they wanted Box2D, but wanted to make it look and act the same as the existing Physics system. It started making a little more sense as I went on (after weeks of screwing with it) but I actually just scrapped the idea of using it on a real project because of all the problems I was having trying to make things work sensibly... YMMV. Bottom line I would say you could use it but you should expect to do a lot of legwork behind the 2D physics system, making it more of a periodical accessory than a feature of your pipeline.
     
  5. shopguy

    shopguy

    Joined:
    Jan 12, 2013
    Posts:
    282
    LaneFox, thanks for the detailed experience. For now I'm trying to work through it because I don't feel like switching to another tool/engine. Hopefully Unity 5 will have more in regards to 2D, since this seems to be the current trend in game development (at least indie or small studios). I have made a lot of progress, but as you said, "a whole lot of customization" and none of the code/settings seem like they would be worth giving away or making a tutorial about, because it would just be a lot of me saying "not sure why this works, but it does" or "this is mostly a hack". It does leave a lot to be desired from a physics engine, for sure not just "rig your car like this and go".
     
  6. CalaveraX

    CalaveraX

    Joined:
    Jul 11, 2013
    Posts:
    143
  7. shopguy

    shopguy

    Joined:
    Jan 12, 2013
    Posts:
    282
    Thanks, that's a nice example of a different method (Hinge joint vs Wheel joint). Still a lot more to do after that basic rigging of course, like making it drive. I'm making a lot of progress now, but was just surprised that something so common (2D car) didn't have some pre-existing/easier methods.
     
  8. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,521
    Not sure if this will help but I'm working on this 2D series for Playmaker and while its catered to Playmaker later on.. there is a some that just covers how to setup and configure joints.

     
  9. shopguy

    shopguy

    Joined:
    Jan 12, 2013
    Posts:
    282
    @LaneFox - Great video, learned a lot of basics... enough to know I really need to play around with all the different joints before deciding which one(s) to use. Made me lol at the end, "whew, that looks fun".