Search Unity

Downforce on collision?

Discussion in 'Scripting' started by gringo2012, Apr 19, 2014.

  1. gringo2012

    gringo2012

    Joined:
    Jul 6, 2012
    Posts:
    46
    Hello guys

    I have a small problem with the collision between 2 vehicles.
    Each vehicle has 2 simple box colliders, a rigidbody and 4 wheel colliders for the wheels.

    When they collide at bigger speeds (not so big though) the cars overlap, like in this picture. Most of the times more violently like in the picture.
    $naspa.png

    I would like to keep them on the ground, only when they collide. Like it happens at very small speeds, see image below.
    $ok.png

    I tried applying a downforce on collision enter but it hasn't changed anything.

    Does anyone have any idea what force I could apply on the colliding cars so that they don't go all crazy like in the first screenshot?

    I did this OnCollisionEnter
    Code (csharp):
    1. rigidbody.AddForceAtPosition(-2000f * transform.up, transform.position)
    Tried with many values, -2, -20, -20000. Got no change whatsoever.

    I'm a bit out of ideas of what I should normally do to stop them from "hugging" each other.

    Thank you
     
  2. pauloaguiar

    pauloaguiar

    Joined:
    May 13, 2009
    Posts:
    700
    using down force turn the car unrealistic, every time the car(s) collide the acts like push down and up wheel suspension and if after, if you set default value on exit collision.

    onCollision Enter

    rigidbody.AddForce(0,-20000,0);

    On.........Exit
    rigidbody.AddForce(0,-0,0);

    use on Update:

    Code (csharp):
    1. this.rigidbody.drag = rigidbody.velocity.magnitude / 250; //250
    2.     this.rigidbody.angularDrag = rigidbody.velocity.magnitude / 250; //250  
     
  3. gringo2012

    gringo2012

    Joined:
    Jul 6, 2012
    Posts:
    46
    Hello pauloaguiar

    Thank you for the reply. Unfortunatelly this does not change the behavior at all. It still works like before.
     
  4. pauloaguiar

    pauloaguiar

    Joined:
    May 13, 2009
    Posts:
    700
    Try this:
    Code (csharp):
    1. var hit : RaycastHit;var heightAboveGround = 0;
    2. var maxheightAbobe : float = 15;
    3. var maxheightDownForce : float = 100;
    4.  
    5.  
    6. function Start () {
    7. }
    8.  
    9.  
    10. function FixedUpdate () {
    11.  
    12.  
    13.     if (Physics.Raycast (transform.position, transform.TransformDirection (Vector3.down) , hit, Mathf.Infinity))
    14.     {
    15.         heightAboveGround = hit.distance;
    16.        
    17.         if (heightAboveGround > maxheightAbobe)
    18.         {  
    19.             this.rigidbody.constraints = RigidbodyConstraints.None;
    20.             this.rigidbody.AddForce(-Vector3.up * maxheightDownForce, ForceMode.Acceleration);
    21.         }
    22.         else
    23.         {              
    24.             this.rigidbody.AddForce(Vector3.up * 0, ForceMode.Acceleration);
    25.         }
    26.     }
    27. }
    It most work, i test it my self and used on my how car racing.
    And i forgot to tell, make sure you car models is not big ( biged scaled),
    As more you model is big more the car physics , behaves like a bird.

    Calculate the scale of you car white the standard +- Box scale.
     
  5. gringo2012

    gringo2012

    Joined:
    Jul 6, 2012
    Posts:
    46
    Thank you for your time.
    Unfortunatelly the results are really bad, as it is pulling the car onto the ground, and it's not a nice view :)

    Seems to be because of the wheel colliders though. I'm getting better results with a Spring set at 9000 and Damper at 500.
    Before I had really low values.

    I am also using an Antiroll script on the wheels to stabilize them a bit.

    I am testing it now with different vehicles and masses but I am already getting much better results while keeping the physics untouched.