- Home /
How to make fall damage using this script right here.
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!
Answer by robcbryant · Mar 18, 2014 at 06:39 AM
You could combine the first two if(grounded) statements into one if block --it's a bit messy/redundant right now. You're making the same 2 logic calls when you only need to make 1. You could do the same for the 3rd if grounded statement.
Also it will only damage the player if it's falling between 90 and 140 secs not 100+ if the player falls for longer than 140 seconds it won't damage the player.
It's also not 140 seconds--it's 140 iterations of the update loop--which may be where you're getting confused? The loop will probably hit 140 iterations in like 5-10 seconds. Try this instead:
//Change your jump logic to save a timestamp of the current time
if (grounded == true)
{
if(Input.GetKey (KeyCode.Space)) {
moveDirection.y = jumpSpeed;
beginJumpTime = Time.time; //make sure you make a float variable for this
}
}
//Now back to your fall damage logic-----
//FALL DAMAGE
//Take this out--you don't even need it--the clock is keeping track of your time for you
//if (grounded == false)
//{
//jumpHeight++;
//}
//get the current time spent in the air
float currentAirTime = Time.time - beginJumpTime;
if (grounded == true && (currentAirTime >= 90.0F && currentAirTime <= 139.0F))
{
PlayerHealth.curHealth -= 25;
}
// You also no longer need to reset this counter--it will automatically reset to a new time stamp the next time the player is grounded and presses the jump button
//else if (grounded == true)
//{
//jumpHeight = 0;
//}