Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

321
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda