Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Telling the time in seconds since the player started falling

Discussion in 'Scripting' started by AcinonX, Sep 27, 2023.

  1. AcinonX

    AcinonX

    Joined:
    Apr 15, 2021
    Posts:
    5
    I am trying to calculate fall damage for the player character in my game I found this formula and implemented it as such:
    Code (CSharp):
    1. fallDamage = (Mathf.Pow(t, 2.0f) / 2.0f) * 5.0f;
    Where 't' is the time in seconds since the player started falling. The "fallDamage" variable is set to 0 when the player is grounded, and set to this formula while the player is falling (using the statement if (!characterController.isGrounded) inside FixedUpdate()).
    The issue is, I don't know how to find 't'! I know how to determine if a certain amount of time has passed, but I can't figure out how to measure the amount of time itself.
    Can someone please help me measure the time in seconds since the player has stopped being grounded during the time when the player is not grounded?
     
  2. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    1,860
    You need to keep a variable that you can update on every frame, accumulating the little fragments of time that pass.

    Code (CSharp):
    1. public float timeFalling;
    2. public float maxSafeFallingTime = 1f;
    3.  
    4. public Update()
    5. {
    6.      bool WeAreFalling = FigureOutIfWeAreFalling();
    7.  
    8.      if (WeAreFalling)
    9.      {
    10.          fallingTime += Time.deltaTime;
    11.      }
    12.      else
    13.      {
    14.          if (fallingTime > maxSafeFallingTime)
    15.              DealFallDamage(fallingTime);
    16.          fallingTime = 0f;
    17.      }
    18.  
    19.      // do whatever else you want
    20.  
    21. }
    You might also decide to calculate the fall distance by accumulating altitude changes, and change the criteria to a maxSafeFallDistance instead.
     
    AcinonX and zulo3d like this.
  3. AcinonX

    AcinonX

    Joined:
    Apr 15, 2021
    Posts:
    5
    In my code, the fallingTime increase occurs in FixedUpdate() (so it changes at a fixed rate). Should I use Time.fixedDeltaTime instead of Time.deltaTime in this case?
     
  4. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    537
    Code (CSharp):
    1.         if (controller.isGrounded)
    2.         {
    3.             if (fallTime>0)
    4.                 fallDamage = (Mathf.Pow(fallTime, 2.0f) / 2.0f) * 5.0f;
    5.             fallTime=0;
    6.         }
    7.         else if (controller.velocity.y<-5) // reached a "falling" velocity?
    8.             fallTime+=Time.deltaTime;
    9.         else
    10.             fallTime=0;
     
  5. CodeRonnie

    CodeRonnie

    Joined:
    Oct 2, 2015
    Posts:
    284
    I'm just glancing over this post, so I'm only really replying to this one comment, but I'm pretty sure Time.deltaTime and Time.fixedDeltaTime are the same value when accessed from within a FixedUpdate(). Confirm for yourself to be sure.
     
    AcinonX, halley and Kurt-Dekker like this.
  6. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    537
    A simpler method?:

    Code (CSharp):
    1.         if (controller.isGrounded && controller.velocity.y<-8)
    2.             DoDamage(controller.velocity.magnitude);