Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 DennisDuty · Jul 07, 2015 at 04:13 AM · scorepowerupmodulus

Using modulus when score climbs in odd integers.

I enable a powerup every 50 points. I would use this in previous builds:

 if (score %50==0 && score !=0)
     powerUp = true;
 

Now I've added score multipliers so "score" no longer increases in increments of 1. It'll climb at 2x, 3x, 4x, etc. Often the score will pass right by 50, 100, 150, 200 without the player ever receiving the power-up.. since "score" isn't ever an exact multiple of 50.

If I use some sort of greater-than check, the powerup is ALWAYS true, rather than just being activated once as a reward.

How do I active the power up ONCE every 50 points?

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

Answer by DennisDuty · Jul 07, 2015 at 04:35 AM

After a few hours the answer I ultimately arrived to was very simplistic.

 private int nextPowerUp = 50;
 
 void powerUpConditions(){
     if (score >= nextPowerUp && powerUp==false)
     {
         powerUp=true;
         nextPowerUp +=50;
     }
 }

The powerUp being a bool ensures only 1 powerup accrues at a time... you either have one or you don't. The # is easy to swap out with a variable if I want the increments to change over time.

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 AlwaysSunny · Jul 07, 2015 at 04:13 AM

Bit longer than necessary, but it's readable. This will only trigger one event per score change. In other words, if your score was 0 and just became 500, the event will only be triggered once and not 10 times. To trigger the event retroactively like that, consider logic which records the difference between the new and old scores, uses a loop to subtract the factor (50) from that difference and triggers the event each iteration, then stops when the remainder is less than the factor.

 private float factor = 50f;
 private float nextScoreLevel = 50f;
 private float score;
 public float Score {
     get { return score; }
     set {            
         if (value >= nextScoreLevel) {
             float rounded = Mathf.Round( value / factor ) * factor;
             nextScoreLevel = ( value < rounded ) ? rounded : rounded + factor;
             // fire your event here, we just hit the next multiple of 50
         }            
         score = value;
     }
 }
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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Score not adding? 3 Answers

multiplayer score profile 1 Answer

Collecting and add points 1 Answer

How to connect a database 1 Answer

Score going up like crazy 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