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 /
avatar image
0
Question by justinkatic · Sep 30, 2017 at 01:36 AM · collidersif-statementsnewbieoverlapspherehealth

keycode and && statment not working?

cant work out why this isn't working it makes sense to me if button pressed and special bar is above 100 then I can activate and special goes to 0 well that works my special drops to 0 and will only drop to 0 when my special is == 100 the problem is as soon as game starts its like my special is always activated regardless of my if statement

 void LateUpdate ()
 {
     if (Input.GetKeyDown (KeyCode.Q) && currentSpecial == 100)
         currentSpecial = minSpecial;

     colliders = Physics.OverlapSphere (transform.position, radius);
 

     foreach (Collider col in colliders) {
         if (col.tag == "Enemy")
             Destroy (col.gameObject);
         else if (col.tag == ("Bullet"))
             Destroy (col.gameObject);
     }
     currentMana = Mathf.Clamp (currentMana, minMana, maxMana);
     if (currentSpecial > maxSpecial)
         currentSpecial = maxSpecial;
     if (currentSpecial < minSpecial)
         currentSpecial = minSpecial;
     UpdateSpecialbar ();
 }

}

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
0
Best Answer

Answer by Bunny83 · Sep 30, 2017 at 09:38 AM

Well, first of all your if statement at the top only affects the line / statement that comes directly after it. So in your case the only conditional code is this line:

 currentSpecial = minSpecial;

That means your OverlapSphere call as well as your foreach loop are executed unconditionally every frame. I guess that's your actual "special" code.

This is actually true for any control flow statements (if, for, while, ...) they only affect the one statement that is follwing. However you can "group" several statements into one block by using curly brackets. You already did this with your foreach loop.

However it's not clear when exactly your "special" should be active. At the moment we can't see where "currentSpecial" is actually increased / decreased.

If you want the effect to only occure "once" when the button is pressed you should simply include your foreach loop and the OverlapSphere in the if body

 if (Input.GetKeyDown (KeyCode.Q) && currentSpecial == 100)
 {
     currentSpecial = minSpecial;
     colliders = Physics.OverlapSphere (transform.position, radius);
  
     foreach (Collider col in colliders)
     {
         if (col.tag == "Enemy")
             Destroy (col.gameObject);
         else if (col.tag == ("Bullet"))
             Destroy (col.gameObject);
     }
 }

However if you want to just "activate" the special and let it last for a few sec you would need some sort of variable to remember the "active" state. You would use that variable to selectively enable / disable the OverlapSphere and foreach loop code. When you press the button to acivate it you would set the active variable to true. When the time ran out you set it back to false.

Note that if currentSpecial is a float value you shouldn't compare it to equality with 100. If you dynamically increase or decrease the value it's unlikely that you ever hit the exact value. If "maxSpecial" is actually 100, then you shouldn't use the magic number "100" in your if statement. Compare it to "maxSpecial" instead.

In general if you compare float values it's better to check a range like

 if ( currentSpecial >= maxSpecial )

This statement is true when currentSpecial is equal or greater than maxSpecial.

It's difficult to actually follow your logic and code as important parts are missing. At the same time the clamping of your "currentMana" value seems misplaced as that value isn't changed in LateUpdate.

It's always better to clamp a value when it's actually changed. This is usually done by providing a set method (either a seperate method or using a property)

 void SetMana(float aNewValue)
 {
     currentMana = Mathf.Clamp (aNewValue, minMana, maxMana);
 }

So when you want to change the value somewhere you would do

 SetMana(currentMana + 5 * Time.deltaTime);

This would increase the value by 5 every second. This ensures at no point in time currentMana can be larger than maxMana or smaller than minMana.

Or as a property:

 public float CurrentMana
 {
    get { return currentMana; }
    set { currentMana = Mathf.Clamp (value, minMana, maxMana);}
 }

And change it like this:

 CurrentMana += 5 * Time.deltaTime;

This would do the same

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 justinkatic · Oct 01, 2017 at 09:36 AM 0
Share

your a legend thanks so much not only fixed my problem but certainly made rest of my code a lot simpler. I did have a regen mana but your method was like 10x better and easier to understand. thanks was stuck on this for 2 days. love this community so good and helpful :D

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

69 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

Related Questions

Physics.OverlapSphere 0 Answers

How To Make Player Health Increase if in a Certain Animation State 1 Answer

Colliders, Triggers, Damage, and Fire 2 Answers

How to cause damage on collision? 1 Answer

Health Bar going 0 right away 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