Search Unity

WheelCollider Collision speed?

Discussion in 'Scripting' started by Aviation_Simmer, Aug 1, 2022.

  1. Aviation_Simmer

    Aviation_Simmer

    Joined:
    Aug 30, 2021
    Posts:
    110
    Hi! I want to make a indicator that shows how hard the airplane landed. I figured out, how to check, if any wheel hits the ground. But I don't know how to get the collision speed...? can anyone help me? thanks!
    Code (CSharp):
    1.     void FixedUpdate()
    2.     {
    3.         foreach (WheelCollider wheel in Wheels)
    4.         {
    5.             WheelHit hit;
    6.             var touched = wheel.GetGroundHit(out hit);
    7.             if (touched)
    8.             {
    9.                 OnTouchDown();
    10.             }
    11.         }
    12.     }
    13.  
    14.     void OnTouchDown()
    15.     {
    16.         //how to get the collision speed of the wheel's?
    17.     }
     
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,647
    You would need to save the Velocity every FixedUpdate and check that saved value once it collides.
     
  3. Aviation_Simmer

    Aviation_Simmer

    Joined:
    Aug 30, 2021
    Posts:
    110
    its not that easy... If I have sloped runways, this method is not correct, because it measures the global velocity but not how fast it collides / moves to the ground!
     
  4. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,647
    You still need to save the Velocity of the previous FixedUpdate. You can then get how fast it's going in the direction of the ground by getting the normal of the collision from the hit value.
     
  5. Aviation_Simmer

    Aviation_Simmer

    Joined:
    Aug 30, 2021
    Posts:
    110
    well.. sorry! I have no idea how I can do this! maybe you can help me?
     
  6. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,647
    You want to Google "Vector Projection".
     
  7. Aviation_Simmer

    Aviation_Simmer

    Joined:
    Aug 30, 2021
    Posts:
    110
    Maybe I can explain it better with this:
    upload_2022-8-1_17-4-22.png
    - airplane going down ~2m/s
    - but runway is sloped upwards
    - it will display 2m/s
    - BUT the Airplane acctually crashes into the runway with ~5m/s!
     
  8. Aviation_Simmer

    Aviation_Simmer

    Joined:
    Aug 30, 2021
    Posts:
    110
    and this is the opposite:
    upload_2022-8-1_17-11-16.png
    - airplane going down ~2m/s
    - but runway is sloped downwards
    - it will display 2m/s
    - BUT the Airplane acctually lands smooth with ~0.2m/s!
     
  9. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,647
    Yep, that's exactly what I thought you were talking about. My solution is the thing you're looking for. What part of it was confusing? I can go more in depth about it.
     
  10. Aviation_Simmer

    Aviation_Simmer

    Joined:
    Aug 30, 2021
    Posts:
    110
    I know how to check if the wheel is touched. I checked out the "Vector Projection" but how and should I even use it? If I want to say it in one sentence, "I need to see the code, how should i write this thing. I have no clue.."
     
  11. Aviation_Simmer

    Aviation_Simmer

    Joined:
    Aug 30, 2021
    Posts:
    110
  12. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,647
    Pretty sure relativeVelocity would just give you the Velocity of the object since the ground isn't moving.

    Unfortunately, I'm not gonna give you the code, but I'll explain the process to you.

    You need the normal of the ground, because it is the direction pointing directly away from the ground. You get this from the WheelHit info you received from GetGroundHit, and that has a normal value you can access.

    Once you have this, and you get the Velocity of the plane once it hits the ground, you can then use the Vector Projection to get the value you're looking for. To quote that link, it is “how much of one vector goes in the direction of the other vector?” and you're trying to get how much of the plane's velocity vector goes in the direction of the normal vector.