- Home /
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
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;
}
}
And yeah, then you can delete m_$$anonymous$$inValue, m_LevelToShow and cirlcesDone
Your answer
Follow this Question
Related Questions
CrossPlatformMobileInput Joystick.cs not working 0 Answers
UI works in editor, but not on mobile device 1 Answer
U-shaped swipe 0 Answers
Moving the cube on a surface. 1 Answer
Button issues since Catalina 0 Answers