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 mei_huang · Dec 18, 2020 at 10:07 AM · scripting problemhealthbar

Healing Zone. 3D game

I have an area where my player would stand and gradually regain health, In this case Its Oxygen because the game is in space. I have my players health gradually decreasing over a time, and when it reaches a specified area the health would regenerate. But the Regeneration script I have automatically shoots it up to full health when I want to see a gradual increase. As well as once it hits full health its begins to decrease again despite still standing in the zone. the colliders notify me that it is hitting, but it doesn't work...

This is what I have

public class PlayerInfo : MonoBehaviour {

 public float MaxOxyLv = 100; 
 public float CurrentOxyLv; 
 
 private const float coef = 2;
 // private const float recoef = 10f;
 public OxyLvl Oxygen; 
   
   
 void Start()
 {   
     
     CurrentOxyLv = MaxOxyLv; 
     Oxygen.SetMaxOxy(MaxOxyLv);
     Oxygen.SetOxy(CurrentOxyLv);
 }

 
 void Update()
 {
     CurrentOxyLv -= coef * Time.deltaTime;
     Oxygen.SetOxy(CurrentOxyLv);

     if (CurrentOxyLv <= 0f)
     {
         Suffocate();
     }
     if (CurrentOxyLv <= 20f)
     {
         print (" LOW OXYGEN WARNING!");
     }

 }

 void OnTriggerEnter(Collider other) 

     {
         if(other.gameObject.tag == "Recover" && CurrentOxyLv < MaxOxyLv)
         {
             Debug.Log ("Refueling");
             Refuel();
         }
         // if(CurrentOxyLv > 99)
         // {
         //     DoNothing();
         // }
     }

void Refuel() { CurrentOxyLv+= 10 *Time.deltaTime; Oxygen.SetOxy(CurrentOxyLv);

 }

// void DoNothing() // { // return; // } void Suffocate() { Debug.Log ("You Lose"); } } I've been working on this for hours....

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

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Jaxcap · Dec 18, 2020 at 12:50 PM

OnTriggerEnter only gets called when the collision first happens, it doesn't keep getting called even if the thing stays inside. Instead, you could use OnTriggerEnter and OnTriggerExit to set a boolean that keeps track of whether your player is inside the refuel area, and then check the boolean in your Update function to see whether you should be gaining or losing fuel.


Something like this:

 bool inRefuelArea;
 
 void Update() {
 
     if (inRefuelArea){
         // Code that makes you regain health
     } else {
         // Code that makes you lose health
     }
 }
 
 void OnTriggerEnter (Collider other){
     if(other.gameObject.tag == "Recover") inRefuelArea = true;
 }
 
 void OnTriggerExit (Collider other){
     if(other.gameObject.tag == "Recover") inRefuelArea = false;
 }
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
1

Answer by mei_huang · Dec 18, 2020 at 01:22 PM

Thank you so much! It works perfectly

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

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

202 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

Related Questions

Access to int from another script 1 Answer

How do I create an undertale player health bar in C #? 1 Answer

Managed to get my character to stay crouched when he has a ceiling over head. Problem is he remains crouched. 1 Answer

i am making an increment of one when user clicks a game object, trying to decrement the counter if the same game object is clicked again 1 Answer

Fetching localized resources. 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