Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

318
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda