Search Unity

How to prevent the Car from tilting/flipping or constrain it to x-z axes?

Discussion in 'Scripting' started by KaiseanGames, Feb 3, 2011.

  1. KaiseanGames

    KaiseanGames

    Joined:
    Jan 31, 2011
    Posts:
    13
    Hi.

    I have some problems with the Car game I am creating.

    I have a simple car (rigidbody) consisting of a car body (a cube with box collider) and 4 wheels (wheel colliders). I am using this simple script for the car movement. It's simple, but problematic.

    The thing is that I want to create a game in which the car movement is constrained to X-Z axes. Even more, the car's wheels should never even detach from the ground. But that is the problem - I don't know how to achieve that. When I enter a turn with too much speed, the car flips. This MUST NOT happen in this game. So I need a reliable way to achieve that.

    I've tried several things. First is playing with the center of the mass, but it seems it does not affect flipping that much. If I set it too low, then the car has difficulties accelerating. And if I increase the engine power to pair that, the same thing happen as before. I've even tried to manually edit the rotations at the end of FixedUpdate, but I was unable to achieve any good results. Only unnatural behaviour (skipping, flipping, unhandled rotations and such).

    The other thing I tried (which is currently in the script) is to apply a down force on a rigid body at positions of all the wheels. This does let me drive faster without flipping, but still making a really bad turn can flip it. I need this movement to be something along the lines of the Death Rally. There you can't enter the turn fast enough to flip, you instead slide. (you can't flip, of course, because it's not a 3D game, but that's what I'm trying to achieve, movement similar to DR). It's important to note that I don't need realistic physics with gears and such. I need the car to be able to reach it's max speed in a second or two and to be able to enter the turn at max speed without ANY tilting (let alone flipping).

    So, basically this boils down to two questions:
    1) How to prevent the car from tilting/flipping while turning at high velocities?
    2) Is there any way (in Unity3D) to constrain the physics to the X-Z axes so that the Y-component of the forces doesn't even affect physics calculations?
     

    Attached Files:

  2. Cameron_SM

    Cameron_SM

    Joined:
    Jun 1, 2009
    Posts:
    915
    Lower the centre of gravity of the car's rigid body.

    Trust me, it works. If you put it low enough it'll even flip itself over if the car ever lands on it's roof. You might need to scale the centre of gravity with velocity or distance from the ground (if your game has jumps) so small bumps still behave in a realistic way. Shawn from Blurst did something similar with his VelociRaptor Safari game, you'd hit jumps and go flying but always land catlike on your wheels.
     
  3. jtman562

    jtman562

    Joined:
    Dec 11, 2010
    Posts:
    368
    I was just working on a project where I had a rigid body on an object and I didn't want it to flip. Here's the script I used:

    Code (csharp):
    1. var xRotationLimit = 20;
    2. var yRotationLimit = 20;
    3. var zRotationLimit = 20;
    4.  
    5. function Update () {
    6.  
    7. if(transform.rotation.eulerAngles.x > xRotationLimit){
    8. transform.rotation = Quaternion.identity;
    9. }
    10.  
    11. if(transform.rotation.eulerAngles.y > yRotationLimit){
    12. transform.rotation = Quaternion.identity;
    13. }
    14.  
    15. if(transform.rotation.eulerAngles.z > zRotationLimit){
    16. transform.rotation = Quaternion.identity;
    17. }
    18. }
    This script says that if an object's rotation exceeds more than a pre determined amount, it makes the rotation of that axis go back to zero. So you could make the rotation limits zero so the car can't tilt at all. I'm sure you'll be able to figure out how to modify it to work with your project. I hope this helps. Thanks.
     
    profitdefiant and lsugzdinis like this.
  4. KaiseanGames

    KaiseanGames

    Joined:
    Jan 31, 2011
    Posts:
    13
    First, I tried to lower it at the lowest point of the car mesh. The same problem. Then I tried to lower it even more - to the Y=0 point. Same problem. I tried to lower it really low, like Y=-10 below the car... Then the car started shaking and tilting and uncontrollably rotating when driving... I understand that flipping has much to do with the center of the mass, however, I don't think that's what's problem here.

    When I do this, the car sometimes won't even drive. It turns strangely, sometimes turns even without me applying any steering.

    I have no idea how to fix this. I guess I don't really understand the way the wheel colliders actually run the rigid body. :(
     
  5. jtman562

    jtman562

    Joined:
    Dec 11, 2010
    Posts:
    368
    I think I know the problem. You see, that script uses the world axis. You want the local axis. The problem is that the script wants to line the car up with the 0 rotation of the whole world. Not the 0 axis of the car. How to change that I don't know, but if you ask about it I'm sure someone can help you. If you have any other questions I'll try to answer them the best I can.
     
    profitdefiant and MrUndead like this.
  6. Navi3D

    Navi3D

    Joined:
    Mar 18, 2010
    Posts:
    17

    Thanks man...

    This strategy works for me...
     
  7. austin_farmer90

    austin_farmer90

    Joined:
    May 4, 2014
    Posts:
    42

    Thats awesome, thanks for posting
     
  8. zero_null

    zero_null

    Joined:
    Mar 11, 2014
    Posts:
    159

    Super thanks my love. Hello from the future. You are an angle.
     
    MARCOSEBASTIANI_93 likes this.
  9. nischalk59

    nischalk59

    Joined:
    Dec 2, 2019
    Posts:
    1

    Omg how can I be so stupid?
    Well this worked for me. Thank You
     
  10. Tempest_mi

    Tempest_mi

    Joined:
    Oct 19, 2021
    Posts:
    1
    Thank you very much it worked very well! what I've done is

    Code (CSharp):
    1. void Start()
    2. {
    3.     rigidbody.centerOfMass += new Vector3(0, -1f, 0);
    4. }
    5.  
     
  11. elaine_unity694

    elaine_unity694

    Joined:
    Oct 12, 2020
    Posts:
    26
    Thank you so much ! This work fine for me
     
  12. fk624178

    fk624178

    Joined:
    Jun 14, 2019
    Posts:
    1
    Thanks man this kinda works but now my car behaves like a pendulum. How do I fix it?
    upload_2023-4-3_0-51-0.png
    upload_2023-4-3_0-51-22.png
     

    Attached Files:

  13. Booja_private_cit

    Booja_private_cit

    Joined:
    Jun 7, 2022
    Posts:
    8
    Thank you - This is great