Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
1
Question by FlyingFlambe · Oct 24, 2016 at 08:32 PM · functiondamageinvulnerabilityhealth

How do I check if a function has been called recently?

Hi, I'm working on a script where the player takes 1 damage when being crushed in between two objects. However, the issue I'm having is that if the objects are traveling fast enough, the player takes 2 damage.

What I'm currently trying to do is check if a function has recently been called, and if so, to not call the function again. I also tried adding an invulnerability function, but I wasn't able to get that to work. Here's the code as it is-- still learning coding in general, so I apologize if there's some bizarre practice going on. Thank you!

PlayerController script:

 if (isCrushed)
         {
             RaycastHit2D hit = new RaycastHit2D();
             hit = Physics2D.Raycast(transform.position, Vector2.up, 5.0f, whatIsHexPlate);
 
             if (hit.collider != null && hit.collider.tag == "HexPlate")
             {
                 hit.collider.gameObject.transform.GetComponent<Rigidbody2D>().AddForce(upHpm);
             }
 
             //Deduct 1 health.
             playerHealth.TakeDamage(1);
         }

PlayerHealth script:

 public void TakeDamage(int amount)
     {
         damaged = true;
         currentHealth -= amount;
 
         if (currentHealth <= 0 && !isDead)
         {
             Death();
         }
     }
 
     public void Invulnerable(float time)
     {
         damaged = false;
         invulnerableTimeMax = time;
 
         if (Time.deltaTime < invulnerableTimeMax)
         {
             damaged = false;
             isInvulnerable = true;
         }
         else if (Time.deltaTime >= invulnerableTimeMax)
         {
             isInvulnerable = false;
         }
     }

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by andzq · Oct 24, 2016 at 10:23 PM

hello FlyingMeta. Making your charakter invulnerable for a short time after hit can be done by various methods. One that I like to use for such things is to make a bool that flips to true and prevents further damaging before flipping back to false.

     public bool invulnerable = false;
     public float invulnerableTime = 2f;

     void Update () {
         if (!invulnerable)
         {
             if (isCrushed)
             {
                 //yourCode
                 //Deduct 1 health.
                 //playerHealth.TakeDamage(1);
                 invulnerable = true;
             }    
         }
         else
         {
             invulnerableTime -= Time.deltaTime;
             if (invulnerableTime <= 0)
             {
                 invulnerableTime = 2f;
                 invulnerable = false;
             }
         }    
     }


you can as well make a coroutine to count down the time and gets started as you are damaged.

     void Update () {
         if (!invulnerable)
         {
             if (isCrushed)
             {
                 //yourCode
                 //Deduct 1 health.
                 //playerHealth.TakeDamage(1);
                 invulnerable = true;
                 StartCoroutine(InvulnerableCountdown(invulnerableTime));
             }
         }
     }
 
     IEnumerator InvulnerableCountdown(float time)
     {
         yield return new WaitForSeconds(time);
         invulnerable = false;
     }


hope it helps (:

Comment
Add comment · Show 2 · 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 FlyingFlambe · Oct 25, 2016 at 01:10 AM 0
Share

Used the first solution and it worked very well, thank you :]

avatar image andzq FlyingFlambe · Oct 25, 2016 at 02:27 AM 0
Share

nice (: you´re welcome

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

75 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 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 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 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

Unity crashes ONLY when this function gets called many times per second. Help would be very appreciated. 0 Answers

Interesting Script Problem 1 Answer

Unity: Player take damage from enemy projectile 2 Answers

How do I do a Immunity time after being hited? 2 Answers

Problems with health system based on hearts 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