Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 Sayuka · Jul 07, 2014 at 09:32 AM · c#transformdamagefalling

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.

Comment
Add comment · Show 2
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 Nerevar · Jul 07, 2014 at 09:44 AM 0
Share

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.

avatar image Sayuka · Jul 07, 2014 at 09:53 AM 0
Share

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?

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Ngoc Ngo · Jul 07, 2014 at 09:41 AM

You may not want to reset lastPositionY on line 64.

Comment
Add comment · Show 1 · 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 Sayuka · Jul 07, 2014 at 10:04 AM 0
Share

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.

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.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Implement the equation as a code? 1 Answer

Conceptual Question on Creating Arrays at Runtime 1 Answer

C# GameObjectList not Setting Parent 0 Answers

Instantiate Object position C#, not visible after new pos 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