Search Unity

First Person Controller fall damage

Discussion in 'Scripting' started by jessee03, Sep 27, 2013.

Thread Status:
Not open for further replies.
  1. jessee03

    jessee03

    Joined:
    Apr 27, 2011
    Posts:
    729
    I've been meaning to add falling damage to Unitys First Person Controller. I am just unsure of the best way to go about doing this. Was thinking of starting a timer when grounded == false, then after lets say 4 seconds it would flag your character to die after grounded == true. Any other suggestions that would work better?
     
  2. Brotherhood

    Brotherhood

    Joined:
    Dec 8, 2012
    Posts:
    73
    You can look at how they do it on the FPSWalkerEnhanced script. Your method would work fine too. Alternatively, you could create a variable that you increment as long as your character isn't grounded then, once you he hts the ground, subtract the variable's value from the player's health.
     
  3. jessee03

    jessee03

    Joined:
    Apr 27, 2011
    Posts:
    729
    For what I'm doing it will just be a kill or no kill type of thing. Not a specific amount of damage
     
  4. Zurusona-Kitsune

    Zurusona-Kitsune

    Joined:
    Mar 11, 2012
    Posts:
    94
    I think the best way would be to create a variable that you increment from the character moving in the negative Y direction (downward).
    Like this you can say if distance fallen > 5 meter then die or something like that.
     
  5. jessee03

    jessee03

    Joined:
    Apr 27, 2011
    Posts:
    729
    Great idea! I could Create two variables. One for beginning to fall which would keep track of the y var. Then when the User hits the ground add that Y position to another var which could be landed. From there compare the two var and if it's greater then a certain amount then kill the Player.
     
  6. jessee03

    jessee03

    Joined:
    Apr 27, 2011
    Posts:
    729
    So I did some messing around with the CharacterMotor and came up with this. Problem is when I jump like normal I get a positive number and when I fall it's always a negative. No matter where I place my terrain on the Y axis I always get this issue with jumping and falling.

    Code (csharp):
    1.  
    2.  
    3. var beginFalling : float;
    4. var landing : float;
    5. var fallDistance : float;
    6. var didJump : boolean;
    7.  
    8. var player : GameObject;
    9.  
    10. var fell : boolean;
    11.  
    12. function Update () {
    13.     if (!useFixedUpdate)
    14.         UpdateFunction();
    15.        
    16.     //FALL DEATH calculation
    17.     if(grounded == false) {
    18.         didJump = false;
    19.        
    20.         if(fell == false) {
    21.             fell = true;
    22.             beginFalling = 0;
    23.             beginFalling = player.transform.position.y;
    24.         }
    25.     }
    26.     if(grounded == true  didJump == false) {
    27.    
    28.         didJump = true;
    29.         landing = player.transform.position.y;
    30.         fallDistance = beginFalling - landing;
    31.        
    32.         Debug.Log(beginFalling);
    33.         Debug.Log(landing);
    34.         Debug.Log(fallDistance);
    35.        
    36.         if(fallDistance > 90) {
    37.        
    38.             Debug.Log("DEAD");
    39.             //Death function
    40.        
    41.         }
    42.        
    43.         landing = 0;
    44.         fallDistance = 0;
    45.        
    46.    
    47.     }
    48.  
    49.  
     
  7. Zurusona-Kitsune

    Zurusona-Kitsune

    Joined:
    Mar 11, 2012
    Posts:
    94
    I think rather than finding where your character start to fall, do something like this:

    Code (csharp):
    1.  
    2. float lastPositionY = 0f;
    3. float FallDistance = 0f;
    4.  
    5. function Update ()
    6. {
    7.   if(grounded)
    8.   {
    9.     FallDistance= 0;
    10.     lastPositionY =0;
    11.   }
    12.   else
    13.   {
    14.     if (lastPositionY > player.transform.position.y)
    15.     {
    16.       FallDistance += lastPositionY  - player.transform.position.y;
    17.     }
    18.     lastPositionY  = player.transform.position.y;
    19.   }
    20. }
    21.  
    In other words when the character move in the negative Y, add the Y distance traveled since the last frame to the FallDistance float.
     
  8. XxNexusxX

    XxNexusxX

    Joined:
    Mar 23, 2017
    Posts:
    4
    Can someone write a standalone script for fall damage?
     
Thread Status:
Not open for further replies.