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 FromRealm · Jun 06, 2020 at 10:54 PM · 2d2d gamebeginnerhealthbarhealth

[2D][Beginner]How do I heal my character when pressing a key while next to a health pack?

I need my character to heal when I press E next to a health pack.

https://pastebin.com/wG0S7v70

Comment
Add comment · Show 1
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 ShadyProductions · Jun 06, 2020 at 11:41 PM 0
Share

use OnCollisionStay ins$$anonymous$$d of OnCollisionEnter

OnCollisionEnter is only triggered once, so you'll have only 1 frame of time to press E key. While OnCollisionStay is triggered continuously when ur in the trigger.

1 Reply

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

Answer by unity_qrj_5UYMolP0jA · Jun 07, 2020 at 12:04 AM

Your problem is that you're only checking for E only when the the player enters the collision for the health pack.

What you need is a bool (I.E. playerContact = true), set to true on collision enter and false on collision exit.

Then in update, check for both input of E, and that your bool is set to true. So the health pack script should look something like this;

 void Update()
 {
         if (playerContact == true && Input.GetKeyDown(KeyCode.E))
         {
                  umbrellaBar.currentHealth += refillValue;
         {
 {
 void OnTriggerEnter2D(Collider2D bump)
 {
     if (bump.name == "Player")
     playerContact = true;
 }
 void OnTriggerExit2D(Collider2D bump)
 {
     if (bump.name == "Player")
     playerContact = false;
 }

EDIT: As Shady Productions pointed out there is also the OnCollisionStay function instead of OnCollisionEnter. https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnCollisionStay.html

 void OnCollisionStay(Collision collisionInfo)
     {
        if(bump.name == "Player" && Input.GetKeyDown(KeyCode.E))
         {
             umbrellaBar.currentHealth += refillValue;
         }
     }

One thing to point out, is that with both solutions however, is a potential bug. If you have AI enemies walking around and they enter the health packs collision, you're not checking it's the actual player you've bumped into, and it's just checking for all collisions. So if an enemy walks into the collision and the player presses E while being far away from the health pack, it will still execute the refill health code. To avoid this, check the collision is with the player using Bump. I've added it to the above code. If your Player game object is named Player, it'll work. Otherwise change "Player" to whatever name the player game object is using.

Comment
Add comment · Show 3 · 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 ShadyProductions · Jun 07, 2020 at 12:19 AM 0
Share

Or you can just use OnCollisionStay ....

avatar image unity_qrj_5UYMolP0jA ShadyProductions · Jun 07, 2020 at 12:24 AM 0
Share

Or that... To be honest, I did think of that after after I posted :/ It's just more of a personal preference of $$anonymous$$e to keep that kind of functionality in Update() so it's easy to find when scripts start getting a bit longer.... But OnCollisionStay would be less overhead.

avatar image FromRealm · Jun 07, 2020 at 12:40 AM 1
Share

It works! Thanks a lot!

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

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

Health subtracts when hit by bullit 2 Answers

Drag and release 2 Answers

Enemy Health Bar UI 1 Answer

Scaling related to damage 2 Answers

How to make sounds louder if I go faster 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