• 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 _ale19_ · Jun 08, 2019 at 12:33 PM · uimobilegamescore system

Circular score bar - a better way?

Hi everyone,

I'm working on my first mobile game with Unity and these days I'm focusing on a circular score system to be shown on the game over menu. This is the solution that I've found:

 public class LevelRing : MonoBehaviour
 {
     [Header("Score Variables")]
     // Score stored as a scriptable object
     public IntVariable CurrentScore;
 
     [Header("Elements")]
     // The image used for the filling
     [SerializeField] private Image m_RingImage;
     // Text Showing the level
     [SerializeField] private TextMeshProUGUI m_LevelText;

     // 5 points per level
     private float m_MaxValue = 5.0f; 
     private float m_MinValue= 0.0f;
 
     private float m_CurrentValue = 0.0f;
     private int m_LevelToShow = 1;
     private int cirlcesDone;
 
     private void Start()
     {
         m_CurrentValue = 0.0f;
         m_LevelToShow = 1;
         cirlcesDone = 0;
     }
 
 
     private void Update()
     {
         UpdateFillRing();
     }
 
     private void UpdateFillRing()
     {
         int numberCompleteCircle = CurrentScore.value / m_MaxValue;
         float remainingPercentage = (CurrentScore.value % m_MaxValue) / m_MaxValue;
 
         if (cirlcesDone < numberCompleteCircle)
         {
             m_CurrentValue = Mathf.Clamp(m_CurrentValue, m_MinValue, m_MaxValue);
 
             if (m_CurrentValue >= m_MaxValue)
             {
                 m_CurrentValue = 0.0f;
                 m_LevelToShow += 1;
                 m_LevelText.text = m_LevelToShow.ToString();
                 cirlcesDone += 1;
             }
 
             float fillPercentage = m_CurrentValue / m_MaxValue;
             m_RingImage.fillAmount = fillPercentage;
 
             m_CurrentValue += Time.deltaTime * 2;
         }
         else
         {
             if (remainingPercentage > 0)
             {
                 if (m_RingImage.fillAmount < remainingPercentage)
                 {
                     m_CurrentValue = Mathf.Clamp(m_CurrentValue, m_MinValue, m_MaxValue);
 
                     float fillPercentage = m_CurrentValue / m_MaxValue;
                     m_RingImage.fillAmount = fillPercentage;
 
                     float t = fillPercentage / remainingPercentage;
 
                     m_CurrentValue += Mathf.Lerp(Time.deltaTime * 2, Time.deltaTime/4, t);
 
                 }
             }
         }
 
     }
 

The code works but I don't feel that is the right approach to it! What do you guys think?

Thanks for the help in advance.

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

Answer by SirPaddow · Jun 08, 2019 at 06:17 PM

Maybe I'm missing something, but I think it could be simplified to this:

 private void UpdateFillRing()
 {
     if (m_CurrentValue < CurrentScore.value)
     {
         // Adapt it to smoothly reach the expected value, 
         // as you did with Mathf.Lerp(Time.deltaTime * 2, Time.deltaTime / 4, t);
         m_CurrentValue += Time.deltaTime * 2;
         m_LevelText.text = ((int)(m_CurrentValue / m_MaxValue)).ToString();
         m_RingImage.fillAmount = (m_CurrentValue % m_MaxValue) / m_MaxValue;
     }
 }
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 SirPaddow · Jun 08, 2019 at 06:19 PM 0
Share

And yeah, then you can delete m_$$anonymous$$inValue, m_LevelToShow and cirlcesDone

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

246 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

Related Questions

UI not scaling up on mobile 1 Answer

Problem TimeScale 4 Answers

How do I effectively use UI buttons to move my character without confusing assets from the asset store? 1 Answer

U-shaped swipe 0 Answers

Moving the cube on a surface. 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