• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by spiralfire11 · Aug 29, 2013 at 09:53 PM · c#collisionontriggerenterfallinghealthbar

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.

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image spiralfire11 · Aug 29, 2013 at 09:45 PM 0
Share

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), "");

} }

avatar image Matthew012 · Aug 29, 2013 at 10:06 PM 0
Share

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.

avatar image getyour411 · Aug 29, 2013 at 10:25 PM 0
Share

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?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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); 
         }
     } 
 } 

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image spiralfire11 · Aug 30, 2013 at 06:55 PM 0
Share

Don't get it...

avatar image sparkzbarca · Sep 01, 2013 at 12:20 PM 0
Share

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.

avatar image spiralfire11 · Sep 09, 2013 at 11:01 PM 0
Share

Could youplease send me an example?

avatar image sparkzbarca · Sep 10, 2013 at 06:44 PM 1
Share

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
avatar image spiralfire11 · Sep 13, 2013 at 02:09 AM 0
Share

??? Oh god this is complicated...

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

On Trigger Enter, Collide with object, specific collision 1 Answer

Problem with OnTriggerExit 1 Answer

Could some one help me with this script... 5 Answers

Collision reaction not working 2 Answers

How to make gameObject make only one collision 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges