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): //Player_Logic.js var health : int = 100; // Set as First Person Controller in the inspector. var player : GameObject; var fallDamage : float = 0; var jumpHeight : float = 0; var maxJumpHeight : float = 64; function Update () { Health(); FallDamage(); } function Health() { // If health is less than to 0, Kill the player. if( health < 0 ) { //Kill the player Destroy (player); } // If health is more than 100, Reset the players health to 100. if( health > 100 ) { //Set the players health back to 100 health = 100; } } function FallDamage() { // TODO : Maybe the i need some sort of collision detection to detect, // if the player is on the ground, because it thinks the player is not on the ground. // If the player is not on the ground.Then add a counter. if(!player.isGrounded) { jumpHeight++; Debug.Log("Player is not on the ground."); } // If the player is on the ground and the jumpheight is more that the maxJumpHeight, // take the jumpheight from the players health. if(player.isGrounded jumpHeight >= maxJumpHeight) { Debug.Log("Taking Health - Jumpheight"); health -= jumpHeight; } // If the player is back on the ground reset the jumpHeight to zero. if(player.isGrounded) { Debug.Log("Player is on the ground!!!."); jumpHeight = 0; } }
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): private var chMotor: CharacterMotor; var ch : CharacterController; var FallingSpeed : int = ch.velocity.y; var hitPoints : float = 100; var healthRegenRate : float = 0.001; var Hurting : boolean = false; var GoingToTakeDamage : boolean = false; var FallSpeedBeforeDamage : float = 20.0; function Start(){ ch = GetComponent(CharacterController); chMotor = GetComponent(CharacterMotor); } function Update(){ FallingSpeed = ch.velocity.y; barDisplay = hitPoints; if (hitPoints > 100){ hitPoints = 100; } if (FallingSpeed < FallSpeedBeforeDamage){ GoingToTakeDamage = true; } Death(); } function FixedUpdate(){ hitPoints += healthRegenRate; if (hitPoints > 100){ hitPoints = 100; } } function Death(){ if (hitPoints <= 0){ Application.LoadLevel(Application.loadedLevel); } } function Heal(points : float) { hitPoints = Mathf.Min(100.0, hitPoints + points); } function OnControllerColliderHit (hit : ControllerColliderHit) { Debug.Log("Hit Something!"); if(GoingToTakeDamage){ hitPoints -= Mathf.Abs(FallingSpeed) / 3; GoingToTakeDamage = false; } }
What is the script applied to? The player controller or the surface that it lands on or something else?
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?