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

Car Mechanics

Discussion in 'Game Design' started by blaupunkt, Dec 13, 2020.

  1. blaupunkt

    blaupunkt

    Joined:
    Jan 5, 2013
    Posts:
    18
    Hello there,

    I have been trying to create a car racing game(simulation) using Unity. I tried using wheel colliders to create a prototype. But i don't find a suitable tutorial dealing with the mechanics of wheel collider. How the different parameters are related to each other.

    This was the tutorial I followed to start with wheel colliders. But when I tweak the same car starts to vibrate.

    My concerns are:
    1. Do we have an advanced tutorial on Car Racing to create a professional game or is it something that we achieve after years of playing with game engines?
    2. Is it better to go for an arcade-style genre when faced with the complexities of simulation type racing games?

    The well off racing game came up with unity by an indie developer as far as my knowledge is this.

    Please shed some light on this area.

    Also, I dirtied my hands using the Standard Asset car provided by Unity Technologies. But I found the code not straight forward. I could not tweak it much apart from just changing the mesh. If there is an explanation given to the same also, it would be really helpful.

    [Not quiet sure whether i posted the thread in the right place, please lemme know will change the same.]
     
    Last edited: Dec 13, 2020
  2. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,464
  3. blaupunkt

    blaupunkt

    Joined:
    Jan 5, 2013
    Posts:
    18
    @polemical :
    Oh! I have missed out on this asset! Let me check on this and get back.
    Thanks a lot for the quick response.
     
    adamgolden likes this.
  4. blaupunkt

    blaupunkt

    Joined:
    Jan 5, 2013
    Posts:
    18
    Thanks a lot for sharing the information. It helped me for a bigger leap.
    Back into the business.

    So I used the anti rollbar scrip from here. (c# script). But the car still flips.

    How do we calibrate these settings in terms of
    1. Car rolling upside down.[ increasing the anti rollbar value turns into car jittering]
    2. The drifting of the car.
    3. How can the car accelerate. Is it directly dependent on the torque?
     
    adamgolden likes this.
  5. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,464
    I still don't have my vehicle physics exactly how I want, unfortunately it's just such a complicated thing to get working well from scratch, vs. vehicle controllers that have been in development for years. That said, DIY is where I'm at as well. Do note my advice is just based on what I've learned so far - if someone else has more to contribute to this thread that would be great :D

    For the anti-roll you will want to scale the antiRollForce by your velocity.magnitude - so at higher speed it has more effect, and when stopped it has none. A good approach to this for your script would be adding something like..
    Code (CSharp):
    1. public AnimationCurve antiRollCurve;
    ..and then you click that in the Inspector and make a curve, add a key for value 0 at time 0 and then your curve based on gameplay testing, so if you wanted it full strength anti-roll at a velocity magnitude of 50 you might add another key with time 50 and value of 1. Also.. make sure you click the options wheel at the bottom right of the curve editor, select "Clamp". Then in your code you can do like:
    Code (CSharp):
    1. var antiRollForce = (travelL - travelR) * AntiRoll * antiRollCurve.Evaluate(rigidbody.velocity.magnitude);
    Another thing I've found that can cause jitter is spring values, you can adjust like this:
    Code (CSharp):
    1. JointSpring js = wheel.suspensionSpring;
    2. js.damper = damperValue;
    3. js.spring = springValue;
    4. wheel.suspensionSpring = js;
    Make sure you do any updates to Wheel Colliders in FixedUpdate (physics update) like the example you're using, as doing any in Update or LateUpdate can also cause jitter. Also make sure your Projects Settings->Time->Fixed Timestep is low enough, the value is in seconds, so if you want a fixed 120fps physics update, you would set that value to 0.0083. Because cars move very fast through the world, you need more frequent physics updates than the default (in my experience at least).

    You can reduce sideways friction of a wheel to make it drift:
    Code (CSharp):
    1. WheelFrictionCurve wfc = wheel.sidewaysFriction;
    2. wfc.stiffness = stiffness;
    3. wheel.sidewaysFriction = wfc;
    Yeah - that was tricky for me as well. I use an AnimationCurve property like noted above to scale torque based on velocity. So if you want to get the car to accelerate fast when it's standing still, you just make the left side of the curve (at time 0) a really high value, then make the curve drop off quickly. You can do the same to make braking work differently depending on how fast you're moving. You could try something like 50000 torque at 0 velocity down to like 100 torque at a velocity of 20, but that's just off the top of my head, you'll need to experiment. You would multiply that value by your input strength, i.e. depending on input system you're using, might be like..
    Code (CSharp):
    1. float torque = Input.GetAxis("Vertical") * torqueCurve.Evaluate(rigidBody.velocity.magnitude);
    ..then you set each drive wheel's .motorTorque = torque.

    There's also forward friction, this is the grip of the tires and impacts acceleration/braking, do same thing as for drifting..
    Code (CSharp):
    1. WheelFrictionCurve wfc = wheel.forwardFriction;
    2. wfc.stiffness = stiffness;
    3. wheel.forwardFriction = wfc;
    And lastly, another tip I can think of to help not rolling over, make sure you're setting your center of mass to the bottom of the car (or even a bit lower).. create like..
    Code (CSharp):
    1. public Transform centerOfMass;
    ..and then drag an empty GameObject that's a child of the vehicle to it in the Inspector. Then you can just drag it up/down/wherever in the editor, and you set the center of mass to that object like this:
    Code (CSharp):
    1. rigidbody.centerOfMass = centerOfMass.localPosition;
     
    blaupunkt likes this.
  6. blaupunkt

    blaupunkt

    Joined:
    Jan 5, 2013
    Posts:
    18
    Seems like need to plunge into this much deeper! Let me go through the workarounds suggested.

    Thanks a lot for taking the time to share your understanding.
     
    adamgolden likes this.
  7. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,418
    My recommendation is go to the Asset Store, try the variety of vehicle physics assets available, and choose the one that best fits your project. That can save you a ton of hours.

    With regards to the WheelCollider, it's mostly a trial & error. You may find some values that work good for one car, but that don't work at all in slightly different car.
    That depends on the type of game you want to create. In my opinion, the simpler that matches your game's requirements, the better.
    That project is using "Arcade Racer: Racing Game Development Kit" from the Asset Store. Vehicle physics were not developed by the indie author, but he uses a pre-made vehicle kit.
     
    adamgolden likes this.