I have a script ,but I can’t figure out how to make fall damage. I have a variable jump height that I had set to raise by 1 every second you are in the air and if you are in the air for more than one hundred seconds you will fall and take damage and then it will reset the variabe jumpHeight(which handled the height) back to zero ,but the thing is it sets it back to zero before it can deal the damage and I don’t know how to fix this? Help please!
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour {
//SPEED AND SUCH
public float speed = 6.0f;
public float strafeSpeed = 5.8f;
public float crouchSpeed = 3.0f;
public float jumpSpeed = 8.0f;
public float gravity = 18.125f;
public float jumpHeight = 0.0f;
private Vector3 moveDirection = Vector3.zero;
private bool grounded = false;
static public bool isRunning = false;
private bool isCrouched = false;
//STAMINA BAR
public GUIStyle staminaBar;
private float maxStamina = 100.0f;
private float curStamina = 100.0f;
private float staminaBarLength;
void Start ()
{
staminaBarLength = 85;
}
void Update ()
{
if (grounded)
{
moveDirection = new Vector3(Input.GetAxis ("Horizontal"),0,Input.GetAxis ("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if(Input.GetKeyDown (KeyCode.LeftShift))
{
speed = 10.00f;
isRunning = true;
}
else if(Input.GetKeyUp (KeyCode.LeftShift))
{
speed = 6.0f;
isRunning = false;
}
}
if (grounded == true)
{
if(Input.GetKey (KeyCode.Space))
{
moveDirection.y = jumpSpeed;
}
}
//FALL DAMAGE
if (grounded == false)
{
jumpHeight++;
}
if (grounded == true && (jumpHeight >= 90 && jumpHeight <= 139))
{
PlayerHealth.curHealth -= 25;
}
else if (grounded == true)
{
jumpHeight = 0;
}
moveDirection.y -= gravity * Time.deltaTime;
var Controller = GetComponent<CharacterController> ();
var flags = Controller.Move (moveDirection * Time.deltaTime);
grounded = (flags & CollisionFlags.CollidedBelow) != 0;
AdjustCurrentStamina (0);
}
void OnGUI()
{
GUI.BeginGroup (new Rect (355, 476, staminaBarLength, 125), string.Empty, staminaBar);
GUI.Label (new Rect (16, 7, 45, 110), "Stamina", staminaBar);
GUI.EndGroup ();
}
void AdjustCurrentStamina(int adj)
{
if (isRunning)
{
curStamina -= Time.deltaTime * 10.0f;
}
if (!isRunning)
{
curStamina += Time.deltaTime * 7.5f;
}
if (curStamina < 0)
{
curStamina = 0;
}
if (curStamina == 0)
{
speed = 6.0f;
if(isRunning == true && curStamina == 0)
{
speed = 6.0f;
}
}
if (curStamina > maxStamina)
{
curStamina = maxStamina;
}
if(maxStamina <1)
maxStamina = 1;
staminaBarLength =(85) * (curStamina / (float)maxStamina);
}
}
The if statements involving jumpHeight are around line 57!