Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Rosebud098 · Mar 16 at 02:47 AM · beginnerweapon systemenergy

How to get my players weapon to not work when its energy bar runs out

Hey there, so i have had no unity experience up until now and im having to create a 2d game for my level design class and i created a mechanism that has the player using a flashlight to defend himself from monsters and it uses an energy bar that has a cool down time and the longer the player holds a certain key down, the bar slowly goes down over time unless the player stops holding down the key. My issue is that the flashlight stays on even if the bar runs out and the player is holding the key down and i want it so that even if the player keeps holding down the key, the flashlight does not work as long as the bar is empty, so the player will have to allow the energy to regen before use. If anyone has an idea how to help, i would really appreciate it. attached is my flashlight script and the script for the energy bar.

 ![using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Flashlight : MonoBehaviour
 {
     [SerializeField] GameObject FlashlightLight;
     
     public bool FlashlightActive = false;
     public PlayerHealth ph;
 
 
     void Start()
     {
         FlashlightLight.gameObject.SetActive(false);
     }
 
     // Update is called once per frame
     private void Update()
     {
         if (FlashlightActive)
             FlashlightBar.instance.UseEnergy(15 * Time.deltaTime);
         if (ph.currentHealth > 0 && Input.GetKeyDown(KeyCode.E))
         {
             FlashlightLight.gameObject.SetActive(true);
             FlashlightActive = true;
         }
 
         if (Input.GetKeyUp(KeyCode.E))
         {
             FlashlightLight.gameObject.SetActive(false);
             FlashlightActive = false;
         }
         if (ph.currentHealth > 0 && Input.GetKeyDown(KeyCode.E))
         {
 
         }
         if (ph.currentHealth < 1)
         {
             FlashlightLight.gameObject.SetActive(false);
             FlashlightActive = false;
         }
     }
     private void OnTriggerEnter2D(Collider2D collision)
     {
         if (collision.tag == "Enemy")
         {
             var healthComponent = collision.GetComponent<DemonBatHealth>();
             if (healthComponent != null)
             {
                 healthComponent.TakeDamage(1);
             }
         }
   
         if (collision.tag == "Enemy")
         {
             var healthComponent = collision.GetComponent<GhostEnemyHealth>();
             if (healthComponent != null)
             {
                 healthComponent.TakeDamage(1);
             }
         }
     }
 }][1]


[1]: /storage/temp/193988-screenshot-2022-03-15-213742.png

screenshot-2022-03-15-213723.png (131.5 kB)
screenshot-2022-03-15-213742.png (111.7 kB)
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
0

Answer by rh_galaxy · Mar 16 at 04:26 AM

Maybe this will work

 //FlashlightBar
 float timer = 0.0f;
 public void UseEnergy(float amount)
 {
     //use up energy
     if(currentEnergy > 0)
     {
         currentEnergy -= amount;
         if(currentEnergy < 0) currentEnergy = 0; //limit
         timer = 0.0f; //reset timer
     }
     flashlightBar.value = currentEnergy;
 }
 public void RegenEnergy()
 {
     //regenerate energy at rate maxEnergy/10.0 per second
     timer += Time.deltaTime;
     if(timer > 2.0f) //begin regenerate after 2 sec
     {
         currentEnergy += (maxEnergy / 10.0f) * Time.deltaTime;
         if(currentEnergy >= maxEnergy) currentEnergy = maxEnergy; //limit
     }
     flashlightBar.value = currentEnergy;
 }
 
 //Flashlight
 private void Update()
 {
     if (FlashlightActive)
         FlashlightBar.instance.UseEnergy(15 * Time.deltaTime);
     FlashlightBar.instance.RegenEnergy();
     FlashlightActive = Input.GetKey(KeyCode.E);
     if (ph.currentHealth <= 0 || FlashlightBar.instance.flashlightBar.value <= 0)
         FlashlightActive = false;
     FlashlightLight.gameObject.SetActive(FlashlightActive);
 }


I think it is easier to do things like this from Update() and not using coroutines. However I think your current code could work if you just add a check for flashlightBar.value and disable the FlashlightLight.gameObject if 0.

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 Rosebud098 · Mar 16 at 03:38 PM 0
Share

so i tried doing this method but im getting alot of angry lines and i know it has something to do with making sure to reference my variables. Im sorry im like super new with coding so im still trying to get an idea of how it all works. I just have issues trying to figure out what to put and where to put it.

[1]: /storage/temp/194004-screenshot-2022-03-16-102608.png

screenshot-2022-03-16-102608.png (172.9 kB)
avatar image
0

Answer by Niter88 · Mar 16 at 04:49 PM

Hello @Rosebud098 this is a good question since it's logic is required in almost every game. At first it is good for you to have the flashilght energy logic inside of the actual Flashlight and not on the UI logic. I would do the logic entirely different, but for simplicity sake:

You could put on the UI script only the logic to change the slider (or other forms of UI) to represent the energy value.

then you could do inside Update:

     if (isPlayerAlive == false) return; //exit the method, this way you can't do weird stuff if dead
     if (Input.GetKeyDown(KeyCode.F)  &&  flashlightEnergy > 0f)
     {
         FireTheFlashlight();
     }
     else
     {
         HoldFireFlashlight();
     }
     
     //outside the Update you could have:
     private void FireTheFlashlight()
     {
         isFlashlightActive= true;
         //logic to fire the flashlight
     }
     private void HoldFireFlashlight()
     {
         if (isFlashlightActive)
         {
              isFlashlightActive= false;
             //logic to stop firing
         }
     }

I changed the variable FlashlightActive to isFlashlightActive to follow the variables convention more accurately. Actually there are many ways to do this, here I present one of the most simple yet efficient way of doing it. You could use a Flashlight.instance on your UI to get the energy value, or, you can at the end of your Flashlight Update send the energy through a method let's say UpdateEnergyBar(float energyLevel) << Note that this one has to be created inside the UI logic and be called inside the Flashlight logic.

Hope it helps you

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

150 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

Related Questions

Unity Mecanim or Blender Animation? 1 Answer

Physics.CheckSphere always returns true when it shouldn't 1 Answer

[Beginner] Using Random.Range in initial game object position 1 Answer

How do I make to print something in console on Unity 5.3 1 Answer

Disable or resizing a clickable cube 0 Answers


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