简体   繁体   中英

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?

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();
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM