Search Unity

Making a car WITHOUT wheel colliders

Discussion in 'Physics' started by allencook200, Jun 22, 2021.

  1. allencook200

    allencook200

    Joined:
    Oct 2, 2020
    Posts:
    178
    Hi, so I'm attempting to create a very simple rigidbody based car in unity, without the physx wheel colliders.

    What I have right now, is a box with a collider and rigidbody, with 4 cylinder colliders as wheels. I simply use addforce to propel the car and that seems to work fine. Next, I added some raycasts to the base of the two front wheels, so that it can detect if there's an object in front of them, which tells the game to add some force upwards to the front of the car, to try to help it climb over whatever the obstacle is in front of it (assuming it isn't too high.)

    Am I on the right track here?

    Another thing I wanted to ask is, how do I get the wheels to rotate correctly? I definitely don't need this car to be super realistic by any means, but I at least want the wheels to rotate proportionally to the car. Is there some sort of formula someone can give me, that will rotate the wheels based on how fast the car is moving?
     
    ashketchum455445 likes this.
  2. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Using cylinders for wheels is not a good idea unless it's strictly visual because the contacts will be very hit and miss for the desired behaviour. It's best to use raycasts / sweeps and maths.
     
  3. allencook200

    allencook200

    Joined:
    Oct 2, 2020
    Posts:
    178
    Unfortunately I don't know much math and I'm just looking for the easiest way to go about doing this. Do you have a barebones example using sweeps or raycasts?

    Edit: Okay so I followed a simple tutorial on youtube with how to do raycasts and it works pretty good, however if force is applied to the side of the car, the car will float freely to the side, but I only want it to move in the direction of the wheels (forward or backwards). Hopefully someone is able to assist me with this.

    Edit #2:

    https://github.com/unity-car-tutori...ty/blob/master/Assets/Scripts/RaycastWheel.cs

    I ended up using this script and it seemly works well with minor modifications, however if anyone has any other examples, please do post them, because there's hardly anything on the internet. It would not only benefit me, but other people in the future looking to work on their own custom vehicles.
     
    Last edited: Jun 22, 2021
  4. LeifStro

    LeifStro

    Joined:
    Mar 23, 2015
    Posts:
    29
    Using sphere colliders as wheels with zero friction works much better than mesh collider wheels. However, you still run into weird ghost collisions on uneven mesh terrain / roads etc. The best solution seams to be something like this simple raycast wheel, except switch it to sphere cast for a better approximation.
     
  5. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    1,928
    This video is a rather good introduction:
     
  6. johnsonrk

    johnsonrk

    Joined:
    Nov 19, 2023
    Posts:
    1
  7. codebiscuits

    codebiscuits

    Joined:
    Jun 16, 2020
    Posts:
    92
    (Mostly adding my context to the stuff said earlier, sorry johnsonrk)
    I talked over here a bit about how I'm using PrismaticJoint springs, zero-friction mesh-colliders & contact modification wheels:
    https://forum.unity.com/threads/please-someone-help-me-understand-joints.1507667/#post-9419714
    There's a great video here that demonstrates possible problems with raycasts:

    I'm not sure I feel like there's a "best" approach. I feel like raycasts would be awkward for the kind of vehicle/terrain behavior I'm trying to implement; but for other stuff I guess they're fine.
    Unless you're very maths&physicsy, or trying to learn because you want to, I'd definitely recommend letting an off-the-shelf solution do most of the work for you. I really like Edy's vehicle physics stuff, except that it's raycast-based (which is only a problem for my very specific requirements) (Edy's currently working on ditching the unity WheelCollider though, so I'm pretty excited:).
    I'm always delighted by this ultra-simple approach:
    https://twitter.com/KenneyNL/status/1107783904784715788?lang=en
    :)