- Home /
Falldamage has no effect on my HB
The Falldamage has no effect on my healthBar when I collide from a distance of 20. here's the script:
Ray GroundedTestRay;
RayCastHit Ground;
RayCastHit PlayerHit;
bool Grounded;
GroundedTestRay.origin = player.transform.position;
GroundedTestRay.direction = -player.transform.up // negative up so down dont forget -
physics.raycast(Ray, out Ground);
GroundedTestRay.origin = Ground.point + (-player.transform.up * .1); //start just under the ground to make sure there isn't any errors from beign so close
GroundedTestRay.direction = player.transform.position - Ground.point; //cast toward player
physics.raycast(GroundedTestRay, out PlayerHit);
if (vector3.distance (Ground.point, PlayerHit.point) > .1)
{
Grounded = false;
ApplyDamage = true;
}
float Timer;
bool TimerEnable;
bool ApplyDamage;
void Update()
{
if(TimerEnable)
{
Timer += time.deltatime;
}
If(!TimerEnable)
{
Timer = 0;
}
void FixedUpdate()
{
if (!Grounded)
{
TimerEnable = true;
}
if( Grounded)
{
TimerEnable = false;
}
if(Grounded && ApplyDamage)
{
if(timer > MinimumTimeToTakeDamage)
{
Damage = (PlayerHealthBar)target.GetComponent("PlayerHealthBar");
eh.AddjustCurrentHealth(-100);
}
}
}
It doen'st work.
Here's the healthbar script : using UnityEngine; using System.Collections;
public class PlayerHealthBar : $$anonymous$$onoBehaviour {
public GameObject target;
GUIStyle style = new GUIStyle();
Texture2D texture;
Color redColor = Color.red;
Color greenColor = Color.green;
private int curHealth = 1000;
private int maxHealth = 1000;
void Start()
{
texture = new Texture2D(1, 1);
texture.SetPixel(1, 1, greenColor);
}
void Update() { AddjustCurrentHealth(0); } public void AddjustCurrentHealth(int adj) { curHealth += adj;
if(curHealth < 0)
{
curHealth = 0;
}
if(curHealth > maxHealth)
{
curHealth = maxHealth;
}
if(maxHealth < 1)
{
maxHealth = 1;
}
if (curHealth > 500) { texture.SetPixel(1, 1, greenColor); } if(curHealth < 10) { Application.LoadLevel ("death"); } if (curHealth < 500) { texture.SetPixel(1, 1, redColor); } }
public void OnGUI() {
texture.Apply();
style.normal.background = texture; GUI.Box(new Rect(10, 10, curHealth, 20), new GUIContent(""), style); GUI.Box(new Rect(10, 10, 1000, 20), "");
} }
Is it suppose to be
public class Falldamage : $$anonymous$$onoBehaviour {
or
public class Falldamage : $$anonymous$$onoBehaviour {};
I don't use C# so I wouldn't know.
This is correct: public class Falldamage : $$anonymous$$onoBehaviour {
The problem might be in this line:
Vector3.Distance(target.transform.position, transform.position)
You setup target as the player but you don't setup a different 'transform' so its this script, what is this script attached to? I'd start by inserting a line right after the Vector3.Distance... line that spits out the values to Debug.Log() so you can verify you are getting the results you intend. Lastly, why do you have this
void Update() { AddjustCurrentHealth(0); }
why bother sending 0 every update/frame?
Answer by craigtrevor · Aug 29, 2013 at 10:36 PM
You have an if statement is your Falldamage class without an else.
Try the following code
private void OnTriggerEnter(Collider other) {
if(other.tag == "player") {
float distance = Vector3.Distance(target.transform.position, transform.position);
if(distance < 20) {
PlayerHealthBar eh = (PlayerHealthBar)target.GetComponent("PlayerHealthBar");
eh.AddjustCurrentHealth(-100);
}
else {
PlayerHealthBar eh = (PlayerHealthBar)target.GetComponent("PlayerHealthBar");
eh.AddjustCurrentHealth(-200);
}
}
}
Answer by sparkzbarca · Aug 29, 2013 at 10:38 PM
A. Distance of 20 is really high first off just to note, it's not the problem but it should be noted that unity uses meters and so 60 feet is not just enough to damage it's enough to kill you.
by the time you collide the distance between the ground and the player is less than 20 so doing a distance check doesnt work.
basically what you actually need to do is during fixed update do a check to see if your grounded if not start a timer.
once you become grounded if the timer is greater than something (you could use the formula for gravity if you want to use distance) so for example
distance = time / 60 (assuming 60 frames per second and updated using fixed update you could use other methods, regardless get the value in seconds) * 9.81;
so if you have fallen for 1 second you have fallen 9.81 or something like 30 feet
then basically do if distance > fall distance take damage.
on collision enter occurs when you collide
according to your code when they collide yuo check to see if distance is greater than 20. Well it's not, it never will be because there already touching. You need to have 2 variables
there start fall time and there end fall time. Right now you can know when they end by collision but you need to note when they start.
The way you do that is note when they stop being grounded.
If they do start a timer, once they get grounded again check the timer to see how much time has passed. Now if you want you can convert time to distance because assu$$anonymous$$g your using earthlike gravity people fall at a rate of 9.8 meters per second so you can take time in seconds and multiply by 9.8 to get the distance they've fallen.
Ray GroundedTestRay; RayCastHit Ground; RayCastHit PlayerHit; bool Grounded;
GroundedTestRay.origin = player.transform.position;
GroundedTestRay.direction = -player.transform.up // negative up so down dont forget -
physics.raycast(Ray, out Ground);
GroundedTestRay.origin = Ground.point + (-player.transform.up * .1); //start just under the ground to make sure there isn't any errors from beign so close
GroundedTestRay.direction = player.transform.position - Ground.point; //cast toward player
physics.raycast(GroundedTestRay, out PlayerHit);
if (vector3.distance (Ground.point, PlayerHit.point) > .1)
{
Grounded = false;
ApplyDamage = true;
}
float Timer;
bool TimerEnable;
bool ApplyDamage;
void Update()
{
if(TimerEnable)
{
Timer += time.deltatime;
}
If(!TimerEnable)
{
Timer = 0;
}
void FixedUpdate()
{
if (!Grounded)
{
TimerEnable = true;
}
if( Grounded)
{
TimerEnable = false;
}
if(Grounded && ApplyDamage)
{
if(timer > $$anonymous$$inimumTimeToTakeDamage)
{
Damage = //whatever you want fall damage to be, however you want to calculate
player.hp -= damage;
}
}
}
something like that }
you note when they stop being on the ground to go ok lets start ti$$anonymous$$g it
you use that time to see if they stayed up long enough to apply damage
then you apply damage if you need to
Your answer
Follow this Question
Related Questions
OnTriggerEnter function giving mixed results 1 Answer
Multiple collisions not working.Plz help 0 Answers
Shooting myself 0 Answers
Destroying object on collision 3 Answers
On Trigger Enter, Collide with object, specific collision 1 Answer