Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

race collisions

Discussion in 'Scripting' started by drakkar33, Nov 10, 2010.

  1. drakkar33

    drakkar33

    Joined:
    Jun 15, 2010
    Posts:
    27
    hello. i'm working on a racing game right now and when my car enter in collision with a wall he dies or else he gets friction and the speed is decremented. The problem is that those collisions aplly to the other cars two when I hit them but I dont want my cars to destroy each other when they collide I want them only to bump each other.


    here is my code

    Code (csharp):
    1.  void OnCollisionStay(Collision death)
    2.     {
    3.         if (death.contacts.Length > 0)
    4.         {
    5.             if (Vector3.Dot(death.contacts[0].normal, floor) < treshold)
    6.             {
    7.                 if (Vector3.Dot(death.contacts[0].normal, moveDirection.normalized) <= fricTreshold  currentSpeed >= 20)
    8.                 {
    9.                     gameObject.SetActiveRecursively(false);
    10.                     currentSpeed = 0;
    11.                     Invoke("Respawn", 2);
    12.                 }
    13.                 else
    14.                 {
    15.                     currentSpeed *= forcedBrake;
    16.                 }
    17.             }
    18.         }
    19.     }
    20.  

    is it better to use a tag or something else?
     
  2. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    I believe you should look at the OnCollisionEnter. Use the relativeVelocity.magnitude of the impact to determine if it is severe enough to cause the player to die, so at a magnitude of 2 or better he gets damaged and his maximum speed is reduced, but at a magnitude of 5 he is no longer capable of racing, thus dead.

    This also being said, you can look at the relativeVelocity vs the relative direction of the vehicle. If he heads straight into the wall, he gets damage modifiers, but if he just grazes it it's not so bad. (the magnitude will kind of show this, but not as well)

    As far as if he hits the wall he slows down, thats a given. After all, you are using a physics engine, let it do its job.
     
  3. drakkar33

    drakkar33

    Joined:
    Jun 15, 2010
    Posts:
    27
    Ty for the tip I'l try this right away :)