Search Unity

Rigidbody vehicle with wheels navigating a grid-based maze

Discussion in 'Physics' started by dewzero, Jan 9, 2019.

  1. dewzero

    dewzero

    Joined:
    Jul 24, 2017
    Posts:
    2
    Hi all,
    I'm pretty new to Unity (though not new to coding or C#) and am struggling with finding the best setup for what I'm trying to achieve... so I thought I'd reach out for help. I've tried many different approaches but nothing seems to work quite "right".

    I want a vehicle (3D) that can go around a grid-based maze (think a pacman layout in 3D) so it pretty much just drives north/south/east/west and the user can only tell it to turn when it gets to a corner (left or right). I'm currently using a rigidbody with raycast suspension and it's being accelerated by using addForce(). Now this is where I'm getting stuck... When the car turns a corner it needs to turn smoothly, not an "about face" like pacman, it should drive a nice quarter-circle turn (ideally with a little drifting and banking for realism). Because I'm using forces to control velocity I don't seem to have enough control to do a precise turn and end up perfectly aligned with the grid. I've read that "correcting" the built in physics isn't such a great idea. I've tried various waypoint based ideas but no luck so far.

    I've been tempted to purchase some kind of traffic or train/rails asset, but they can be quite expensive and I'm not sure they would quite do the trick and retain the realism of a car, with suspension, sliding around a corner.

    Anyway, I've been pulling my hair out on this one. I'm probably over thinking it - I certainly don't know what I don't know ;) I'd love to hear from someone much more experienced... how would you approach this car-in-a-maze scenario?

    Cheers.
     
  2. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    1,459
    Hi, and welcome to Unity forums!

    First I'd create a car you can drive manually, and behave as you expect. You may try any approach here:
    • Single Rigidbody.
    • Rigidbody car + Rigidbody wheels attached with Joints.
    • Rigidbody + WheelCollider.
    • Any vehicle physics solution available in the Asset Store. I recommend you mine, see my signature, but this is a biased opinion ;) Always choose the one that better fits your case.
    The point is that you must be able to drive the car in your scenario using the keys for steering, accelerating and braking, and it must react like you expect. Even if it's a simple solid block sliding on the ground.

    Once your vehicle is working then develop the auto-driving feature. For example, it would be relatively simple to write an script that keeps the vehicle on a straight line, and/or "pursue" a target Transform that is always a few meters ahead of the car. When this targets suddenly changes direction, the car would do so, with more or less "smoothness" based on how you move the target.

    For example, Edy's Vehicle Physics includes a script that applies a random input to the car, so you can see a car driving on its own in the demo scene. This script may be used as example or placeholder for writing any code that drives the car.

    Hope this helps!
     
  3. dewzero

    dewzero

    Joined:
    Jul 24, 2017
    Posts:
    2
    Thanks Edy, I've heard a lot of good about your asset ;) I can drive the car I have manually, and it does behave quite well (a bit arcade-y which works for this). So I'll look into your suggestion of following a transform ahead of the car. That sounds like it could work quite well, while keeping things simple. Much appreciated!
     
    Edy likes this.
  4. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    1,459
    My pleasure! I'm happy to help.