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

How to make 2 Stage 'Fall Damage' System ? (SOLVED)

Discussion in 'Scripting' started by Digital-Phantom, Dec 28, 2014.

  1. Digital-Phantom

    Digital-Phantom

    Joined:
    May 31, 2013
    Posts:
    42
    I have a system in place that allows my player (FPS Controller) to take health damage if it falls more than a certain distance. I'm trying to make it into a 2 stage system where a higher fall would result in greater health loss.
    Basic script-
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var lastPositionY : float = 0f;
    4. var fallDistance : float = 0f;
    5. var player : Transform;
    6.  
    7. private var controller : CharacterController;
    8.  
    9. var currentHealth : float = 100.0f;
    10.  
    11. function Start()
    12. {
    13.     controller = GameObject.Find("First Person Controller").GetComponent(CharacterController);
    14. }
    15.    
    16. function Update ()
    17. {  
    18.     if(lastPositionY > player.transform.position.y)
    19.     {
    20.         fallDistance += lastPositionY - player.transform.position.y;
    21.     }
    22.  
    23.     lastPositionY = player.transform.position.y;
    24.  
    25.     if(fallDistance >= 5 && controller.isGrounded)
    26.     {
    27.         currentHealth -= 20;
    28.         ApplyNormal();
    29.     }
    30.        
    31.     if(fallDistance <= 5 && controller.isGrounded)
    32.     {
    33.         ApplyNormal();
    34.     }
    35. }
    36.  
    37. function ApplyNormal()
    38. {
    39.         fallDistance = 0;
    40.         lastPositionY = 0;
    41. }
    42.  
    43. function OnGUI()
    44. {
    45.     GUI.Box(Rect(10, 20, 50, 20),"" + currentHealth);
    46. }
    This works fine as it is. I changed the script by adding an additional IF statement so as any fall greater than 10 would result in greater health loss BUT the script only recognises the first IF statement (any fall >=5) and not the second.

    revamp script-
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var lastPositionY : float = 0f;
    4. var fallDistance : float = 0f;
    5. var player : Transform;
    6.  
    7. private var controller : CharacterController;
    8.  
    9. var currentHealth : float = 100.0f;
    10.  
    11. function Start()
    12. {
    13.     controller = GameObject.Find("First Person Controller").GetComponent(CharacterController);
    14. }
    15.    
    16. function Update ()
    17. {  
    18.     if(lastPositionY > player.transform.position.y)
    19.     {
    20.         fallDistance += lastPositionY - player.transform.position.y;
    21.     }
    22.  
    23.     lastPositionY = player.transform.position.y;
    24.  
    25.     if(fallDistance >= 5 && controller.isGrounded)
    26.     {
    27.         currentHealth -= 20;
    28.         ApplyNormal();
    29.     }
    30.  
    31.     if(fallDistance >= 10 && controller.isGrounded)
    32.     {
    33.         currentHealth -= 50;
    34.         ApplyNormal();
    35.     }
    36.    
    37.     if(fallDistance <= 5 && controller.isGrounded)
    38.     {
    39.         ApplyNormal();
    40.     }
    41. }
    42.  
    43. function ApplyNormal()
    44. {
    45.         fallDistance = 0;
    46.         lastPositionY = 0;
    47. }
    48.  
    49. function OnGUI()
    50. {
    51.     GUI.Box(Rect(10, 20, 50, 20),"" + currentHealth);
    52. }
    Any suggestions guys ?
     
  2. Digital-Phantom

    Digital-Phantom

    Joined:
    May 31, 2013
    Posts:
    42
    Solved it myself :)

    Why I didn't think to swap the IF statements around so as the first condition is the higher then if that condition wasn't met a simple 'else' statement allowed the script to check IF the second condition was met.

    here is the working script-
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var lastPositionY : float = 0f;
    4. var fallDistance : float = 0f;
    5. var player : Transform;
    6.  
    7. private var controller : CharacterController;
    8.  
    9. var currentHealth : float = 100.0f;
    10.  
    11. function Start()
    12. {
    13.     controller = GameObject.Find("First Person Controller").GetComponent(CharacterController);
    14. }
    15.    
    16. function Update ()
    17. {  
    18.     if(lastPositionY > player.transform.position.y)
    19.     {
    20.         fallDistance += lastPositionY - player.transform.position.y;
    21.     }
    22.    
    23.     lastPositionY = player.transform.position.y;
    24.    
    25.     if(fallDistance >= 10 && controller.isGrounded)
    26.     {
    27.         currentHealth -= 50;
    28.         ApplyNormal();
    29.     }
    30.    
    31.     else
    32.    
    33.     if(fallDistance >= 5 && controller.isGrounded)
    34.     {
    35.         currentHealth -= 20;
    36.         ApplyNormal();
    37.     }
    38.      
    39.     if(fallDistance <= 5 && controller.isGrounded)
    40.     {
    41.         ApplyNormal();
    42.     }
    43. }
    44.  
    45. function ApplyNormal()
    46. {
    47.         fallDistance = 0;
    48.         lastPositionY = 0;
    49. }
    50.  
    51. function OnGUI()
    52. {
    53.     GUI.Box(Rect(10, 20, 50, 20),"" + currentHealth);
    54. }
    (hopefully anybody else having similar issues may find this helpful)

    ;)
     
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Do it backwards. Check the greater then 10 first, then the greater then 5.

    You can also check velocity. Velocity is directly related to distance.
     
    Digital-Phantom likes this.
  4. Digital-Phantom

    Digital-Phantom

    Joined:
    May 31, 2013
    Posts:
    42
    Yeah, swapping them round worked. Thanks for the reply though anyway :)
     
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Sniped by the OP. I was about three seconds behind your post :)
     
    Digital-Phantom likes this.