Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

317
Views
How can I tell when a number has finished increasing?

I feel like I'm missing some easy solution here, but I'm stuck on this. I'm calculating a score based on how far the player travels until they hit a building at the end of the course. The destruction score is separate from the distance score, and it increments until all of the building pieces have come to a rest.

I have an animation I want to play to add the distance score to the total destruction score and give the player's overall score, but I need the animation to trigger once the destruction score has stopped increasing. Right now, each piece of the building has code that checks if its moving and increments the score while true.

public class SkiLodgeScoreTracker : MonoBehaviour
{
    Rigidbody rb;
    private GameObject[] score;
    private void Start()
    {
        rb = gameObject.GetComponent<Rigidbody>();
        score = GameObject.FindGameObjectsWithTag("Score"); 
    }
    private void Update()
    {
        //check if lodgePiece is moving, while it is, add to Score object
        if(rb.velocity.magnitude >= 2f && !rb.isKinematic)
        {
            score[0].GetComponent<SkiScore>().addToSkiScore(2f);
        }

    }
}

Here's where I want to have the animation trigger once that score has stopped (this was another attempt I made, the logic doesn't work)


    public Animator moveScore;
    ...
    if(skiScore > 0)
        {
            previousScore2 = previousScore1;
            previousScore1 = skiScore;
            if(previousScore1 == previousScore2 && !scoreMoved)
            {
                moveScore.SetTrigger("EndCourse");
                addScoreToSkiScore();
            }
            
        }
     public void addScoreToSkiScore()
        {
            scoreMoved = true;
            for(float i = score; i>0; i--)
            {
                skiScore += 1; 
            }
        }

I wanted to grab the score on one frame and see if it equals the score on the next frame and, if so, then trigger the animation, but I feel like that's not a valid option.

Any ideas?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

In SkiScore, keep track of how many pieces are moving, and when a piece stops and the count becomes 0, do your thing:

public class SkiScore : MonoBehaviour 
{
    int movingCount;
    void Start()
    {
        ResetMovingCount();
        /* ... */
    }
    public void ResetMovingCount() {movingCount = 0;} // call as needed
    public void OnStartedMoving() {++movingCount;}
    public void OnStoppedMoving() 
    {
        if (--movingCount == 0) OnNoneMoving();
    }
  
    void OnNoneMoving() {/* do the thing */}

    /* ... */
}

For each piece, use a flag to remember if it has moved recently and if it has, and it's no longer moving, let your score manager know:

public class SkiLodgeScoreTracker : MonoBehaviour
{
    Rigidbody rb;
    private GameObject[] score;

    // flag used to recognize newly stopped movement
    bool recentlyMoving;

    // cache for GetComponent
    SkiScore mySkiScore

    private void Start()
    {
        rb = gameObject.GetComponent<Rigidbody>();
        score = GameObject.FindGameObjectsWithTag("Score"); 

        // GetComponent is expensive, try not to call it in Update unless necessary
        mySkiScore = score[0].GetComponent<SkiScore>();
    }
    private void Update()
    {
        //check if lodgePiece is moving, while it is, add to Score object
        if(rb.velocity.magnitude >= 2f && !rb.isKinematic)
        {
            mySkiScore.addToSkiScore(2f);
            recentlyMoving = true;
            mySkiScore.OnStartedMoving();
        }
        else if (recentlyMoving)
        {
            recentlyMoving = false;
            mySkiScore.OnStoppedMoving();
        }
    }
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!