Search Unity

Fall Damage (Java)

Discussion in 'Scripting' started by irishstorm, Oct 15, 2012.

  1. irishstorm

    irishstorm

    Joined:
    Jun 25, 2012
    Posts:
    8
    Hey there!
    I'm fairly new to java, I'm working on a basic fall damage script and i am having some trouble with the ground.

    I'm using isGrounded that i seen on the API, it seems to be half working and I'm not sure what i have done wrong, it will print all the time even when the player is on the ground, maybe i am not using it correctly or maybe i need to use collision detection?

    Here is the code!
    Code (csharp):
    1. //Player_Logic.js
    2. var health : int = 100;
    3.  
    4. // Set as First Person Controller in the inspector.
    5. var player : GameObject;
    6.  
    7. var fallDamage : float = 0;
    8. var jumpHeight : float = 0;
    9. var maxJumpHeight : float = 64;
    10.  
    11. function Update ()
    12. {
    13.     Health();
    14.     FallDamage();
    15. }
    16.  
    17. function Health()
    18. {
    19.     // If health is less than to 0, Kill the player.
    20.     if( health < 0 )
    21.     {
    22.         //Kill the player
    23.         Destroy (player);
    24.     }
    25.    
    26.     // If health is more than 100, Reset the players health to 100.
    27.     if( health > 100 )
    28.     {
    29.         //Set the players health back to 100
    30.         health = 100;
    31.     }
    32. }
    33.  
    34. function FallDamage()
    35. {
    36.     // TODO : Maybe the i need some sort of collision detection to detect,
    37.     // if the player is on the ground, because it thinks the player is not on the ground.
    38.    
    39.     //  If the player is not on the ground.Then add a counter.
    40.     if(!player.isGrounded)
    41.     {
    42.         jumpHeight++;
    43.         Debug.Log("Player is not on the ground.");
    44.     }
    45.    
    46.     // If the player is on the ground and the jumpheight is more that the maxJumpHeight,
    47.     // take the jumpheight from the players health.
    48.     if(player.isGrounded  jumpHeight >= maxJumpHeight)
    49.     {
    50.         Debug.Log("Taking Health - Jumpheight");
    51.         health -= jumpHeight;
    52.        
    53.     }
    54.    
    55.     // If the player is back on the ground reset the jumpHeight to zero.
    56.     if(player.isGrounded)
    57.     {
    58.         Debug.Log("Player is on the ground!!!.");
    59.         jumpHeight = 0;
    60.     }
    61. }
     
  2. CrazySi

    CrazySi

    Joined:
    Jun 23, 2011
    Posts:
    538
    Not. Java. UnityScript.
     
  3. chrisall76

    chrisall76

    Joined:
    May 19, 2012
    Posts:
    667
    I handled mine a bit differently, making it where it checks your fall speed and if you go over that it will make you take damage depending on the height.
    Code (csharp):
    1.  
    2. private var chMotor: CharacterMotor;
    3. var ch : CharacterController;
    4.  
    5. var FallingSpeed : int = ch.velocity.y;
    6. var hitPoints : float = 100;
    7. var healthRegenRate : float = 0.001;
    8. var Hurting : boolean = false;
    9. var GoingToTakeDamage : boolean = false;
    10. var FallSpeedBeforeDamage : float = 20.0;
    11.  
    12. function Start(){
    13.     ch = GetComponent(CharacterController);
    14.     chMotor = GetComponent(CharacterMotor);
    15. }
    16.  
    17. function Update(){
    18.     FallingSpeed = ch.velocity.y;
    19.     barDisplay = hitPoints;
    20.     if (hitPoints > 100){
    21.         hitPoints = 100;
    22.     }
    23.    
    24.     if (FallingSpeed < FallSpeedBeforeDamage){
    25.         GoingToTakeDamage = true;
    26.     }
    27.     Death();
    28. }
    29.  
    30. function FixedUpdate(){
    31.     hitPoints += healthRegenRate;
    32.    
    33.     if (hitPoints > 100){
    34.         hitPoints = 100;
    35.     }
    36. }
    37.  
    38. function Death(){
    39.     if (hitPoints <= 0){
    40.         Application.LoadLevel(Application.loadedLevel);
    41.     }
    42. }
    43.  
    44. function Heal(points : float) {
    45.     hitPoints = Mathf.Min(100.0, hitPoints + points);
    46. }
    47.  
    48. function OnControllerColliderHit (hit : ControllerColliderHit) {
    49.     Debug.Log("Hit Something!");
    50.     if(GoingToTakeDamage){
    51.         hitPoints -= Mathf.Abs(FallingSpeed) / 3;
    52.         GoingToTakeDamage = false;
    53.     }
    54. }
    55.  
    56.  
     
  4. incenseman

    incenseman

    Joined:
    Nov 20, 2012
    Posts:
    90
    What is the script applied to? The player controller or the surface that it lands on or something else?
     
  5. Tope

    Tope

    Joined:
    Mar 28, 2012
    Posts:
    23
    Why don't you set up a timer to know how much time the player wasn't grounded, and then apply fall damage corresponding to it?