- Home /
FallDamage being given continously instead of just once
I just tried adding FallDamage to my 2.5D Sidescroller. Though instead of taking the damage just once, the Player recieves the damage every single frame until death, no matter what.
My FallDamage script looks like this:
using UnityEngine;
using System.Collections;
public class FallDamage : MonoBehaviour {
public float lastPositionY = 0f;
public float fallDistance = 0f;
public Transform player;
public GameObject Player;
public CharacterController controller;
public void Awake()
{
//controller = GameObject.FindGameObjectWithTag("Player").GetComponent(CharacterController);
CharacterController controller = GetComponent<CharacterController>();
Player = GameObject.FindGameObjectWithTag("Player");
}
public void Update ()
{
CharacterController controller = GetComponent<CharacterController>();
Player pstats = Player.GetComponent<Player> ();
if(lastPositionY > player.transform.position.y)
{
fallDistance += lastPositionY - player.transform.position.y;
}
lastPositionY = player.transform.position.y;
if(fallDistance >= 5 && fallDistance < 10 && controller.isGrounded)
{
//pstats.CurrentHealth = pstats.CurrentHealth / 2;
pstats.CurrentHealth -= 2;
fallDistance = 0;
ApplyNormal();
}
if(fallDistance > 10 && fallDistance < 15 && controller.isGrounded)
{
//pstats.CurrentHealth = pstats.CurrentHealth / 4;
pstats.CurrentHealth -= 4;
fallDistance = 0;
ApplyNormal();
}
if(fallDistance > 15 && controller.isGrounded)
{
//pstats.CurrentHealth = 0;
ApplyNormal();
}
if(fallDistance < 5 && controller.isGrounded)
{
ApplyNormal();
}
}
public void ApplyNormal()
{
fallDistance = 0;
lastPositionY = 0;
}
}
I don't see at all why the damage keeps getting dished out and doesn't stop. What am I missing?
Edit:
Instead of directly doing the damage I also tried creating methods in my Player-script that do the damage and just call them in my FallDamage-script... still the same issue.
Hello,
use a Debug.Log on fallDistance to see what is going on.
I also think it should be :
using UnityEngine;
using System.Collections;
public class FallDamage : $$anonymous$$onoBehaviour {
public float lastPositionY = 0f;
public float fallDistance = 0f;
public Transform player;
bool StartFalling = false;
public GameObject Player;
public CharacterController controller;
public void Awake()
{
//controller = GameObject.FindGameObjectWithTag("Player").GetComponent(CharacterController);
CharacterController controller = GetComponent<CharacterController>();
Player = GameObject.FindGameObjectWithTag("Player");
}
public void Update ()
{
CharacterController controller = GetComponent<CharacterController>();
Player pstats = Player.GetComponent<Player> ();
if(!controller.isGrounded && StartFalling == false){
lastPositionY = player.transform.position.y;
StartFalling = true;
}
if(lastPositionY > player.transform.position.y && !controller.isGrounded)
{
fallDistance += lastPositionY - player.transform.position.y;
}
lastPositionY = player.transform.position.y;
if(fallDistance >= 5 && fallDistance < 10 && controller.isGrounded)
{
//pstats.CurrentHealth = pstats.CurrentHealth / 2;
pstats.CurrentHealth -= 2;
fallDistance = 0;
ApplyNormal();
}
if(fallDistance > 10 && fallDistance < 15 && controller.isGrounded)
{
//pstats.CurrentHealth = pstats.CurrentHealth / 4;
pstats.CurrentHealth -= 4;
fallDistance = 0;
ApplyNormal();
}
if(fallDistance > 15 && controller.isGrounded)
{
//pstats.CurrentHealth = 0;
ApplyNormal();
}
if(fallDistance < 5 && controller.isGrounded)
{
ApplyNormal();
}
}
public void ApplyNormal()
{
fallDistance = 0;
StartFalling = false;
}
}
you also might not need line 31.
I just thought this but it is not tested :p.
I put that in the "ApplyNormal()" method. Now getting quite surprised by the results. It keeps swapping between 0 and 5,9... so the fallDistance is: 0 in the first frame, 5,9 in the second, then again 0, again 5,9 and so on.
Edit: well, I don't quite see the reason for the StartFalling yet.. I mean, the isGrounded is just there to define "you hit the ground", isn't it?
Answer by Ngoc Ngo · Jul 07, 2014 at 09:41 AM
You may not want to reset lastPositionY on line 64.
Wow... that did it actually. I'm surprised cause I took most of the script from an online example where it works perfectly.
Eitherway, thanks for the quick reply, it works just fine without the reset.