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

Question how to calculate the impact speed

Discussion in 'Scripting' started by Aviation_Simmer, Apr 25, 2022.

  1. Aviation_Simmer

    Aviation_Simmer

    Joined:
    Aug 30, 2021
    Posts:
    110
    I am working on a flight simulator and I want to know, how I can measure the landing hardness, called vertical speed (vs) or feet per minute(fpm). I have a script which already displays the vertical speed. But I just want to see the vertical speed, if any of the wheelcolliders touched the ground.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.Threading;
    4. using UnityEngine;
    5.  
    6. public class LandingDecentRate : MonoBehaviour
    7. {
    8.     public float FPM; //descent rate
    9.  
    10.     public List<WheelCollider> Wheels;
    11.     public Text OveralScoreText;
    12.     public GameObject ScorePannel;
    13.  
    14.     public Rigidbody Rigidbody { get; internal set; }
    15.     Rigidbody rb;
    16.  
    17.     private void Awake()
    18.     {
    19.         rb = GetComponent<Rigidbody>();
    20.     }
    21.  
    22.     void Update()
    23.     {
    24.         if () //if the wheelcolliders touch the ground?
    25.         {
    26.             FPM = vs; //how can i measure the vertical velocity at the time of impact?
    27.             ScorePannel.SetActive(true);
    28.            
    29.         }
    30.         else
    31.         {
    32.             FPM = 0f;
    33.         }
    34.  
    35.         if (ScorePannel.SetActive == true)
    36.         {
    37.             Thread.Sleep(5000);
    38.             ScorePannel.SetActive(false);
    39.         }
    40.  
    41.         //----------------------vs
    42.  
    43.         float verticalspeed = rb.velocity.y;
    44.  
    45.         float vs = verticalspeed * 196.85f; // 1m/s = 196.85fpm
    46.  
    47.         // -- Hard Landing Score -- \\
    48.         if (FPM < 180)
    49.         {
    50.             OveralScoreText.text = "Hard";
    51.         }
    52.  
    53.         // -- Normal Landing Score -- \\
    54.         if (FPM > -180 && FPM < -100)
    55.         {
    56.             OveralScoreText.text = "Okay";
    57.         }
    58.         if (FPM > -100 && FPM < -60)
    59.         {
    60.             OveralScoreText.text = "Good";
    61.         }
    62.  
    63.         // -- Butter Landing Score -- \\
    64.         if (FPM > -60)
    65.         {
    66.             OveralScoreText.text = "Butter";
    67.         }
    68.         if (FPM > -30)
    69.         {
    70.             OveralScoreText.text = "very Butter";
    71.         }
    72.         if (FPM > -10)
    73.         {
    74.             OveralScoreText.text = "extreme Butter";
    75.         }
    76.         if (FPM > -1)
    77.         {
    78.             OveralScoreText.text = "Butter King";
    79.         }
    80.     }  
    81. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    This is a tricky area, as I have done it on my Jetpack Kurt, Pilot Kurt and Impulse One games, and in each case it required lots of playtesting and noodling to get it to feel fair.

    You can use fields you get back from the
    Collision
    object itself, such as
    .relativeVelocity
    or
    .impulse
    .

    You can also dig into the
    .contacts
    array to study each individual
    ContactPoint
    for its
    .normal
    , then use that to determine what fraction of the
    .relativeVelocity
    is smash and what is slide.
     
  3. Aviation_Simmer

    Aviation_Simmer

    Joined:
    Aug 30, 2021
    Posts:
    110
    Thanks! I'll try that.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    I forgot to also add that there may be multiple impacts from a single landing event, and each impact may have multiple collisions, and may involve different colliders, and it may occur over several frames.

    The way I ultimately did it for Jetpack Kurt, which has the most-stringent needs (I give you a grade score based on how hard you slam it down), was to observe the sum of the impacts over a few frames, while discounting the total each frame, as well as each collider involved in a given impact.

    I had to do this because I found if I slammed down slightly to one side and only hit one skid, then the other skid rotated and impacted (think helicopter), that would make vastly-different total impulse values than if both skids banged in the same frame.

    Ultimately what I did works "well enough" but it still has some irregularities depending on how you hit, what hits, etc.

    Looks like this: