简体   繁体   中英

How can I calculate and reset Drift Score in Unity?

I hope you are doing well. I need help with a problem. I am working on racing game and in it i want to update score while drifting. I want reset my score ie updatingScore, the score that will attained while driting and then added to the mainScore to 0. So far, I believe I have managed to do that. But the main score resets to 0 as well when I drift again. What can I do to fix it?

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;
public class ScoreManager : MonoBehaviour
{
    public int mainScore;
    public int updatingScore;
    private int driftScore;
    
    public Text updatingScoreText;
    public Text mainScoreText;

    private GameManager gameManager;
    private RCC_CarControllerV3 carController;
    private void Awake()
    {
        gameManager = GetComponent<GameManager>();
     
        updatingScoreText.DOFade(0, 0);
    }

    private void Start()
    {
        mainScore = 0;
        driftScore = 0;
        updatingScore = 0;
    }

    private void Update()
    {
        if(carController == null)
            carController = FindObjectOfType<RCC_CarControllerV3>();
        
        UpdateScore();
        
        CarDrift();
        //ResetScore();
        
    }

    void UpdateScore()
    {
        mainScoreText.text = mainScore.ToString();
        updatingScoreText.text = updatingScore.ToString();
    }

    void ResetScore()
    {
            updatingScore = 0;
            driftScore = 0;
            updatingScoreText.text = updatingScore.ToString();
    }
    void CarDrift()
    {
        if (carController != null)
        {
            if(carController.driftingNow == true)
            {
                updatingScoreText.DOFade(1,2f);
                updatingScore++;
                driftScore = updatingScore;

                mainScore = driftScore;
                StartCoroutine(ScoreReset());
            }
            
            else if(carController.instance.driftingNow == false)
            {
                updatingScoreText.DOFade(0,2f);
            }
        }
        
    }

    IEnumerator ScoreReset()
    {
        yield return new WaitForSeconds(2f);
        ResetScore();
    }
}
mainScore = driftScore;

Sets the main score to the Value. If you want to add the drift score to the main value you need to do it like this

mainScore+= driftScore;

That should do the trick.

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