简体   繁体   中英

How to create a score multiplier?

I am a making an endless skiing game, and already have a scoring system, however i want to add a score multiplier based on the number of consecutive tricks done without touching the ground. here's my script so far:

public class tricksScore : MonoBehaviour
{
    private float flips = 0;
    private float deltaRotation = 0;
    private float currentRotation = 0;
    private float WindupRotation = 0;
    public static Rigidbody2D rigbod;
    public Text scores;
    private int trickscore;
    private int iflip;
    private int oldscore;
    private int incInScore;
    public float speed;
    private float counter;
    private int flipscore;
    private int rockDestroy;
    private bool grounded;
    private int multiplier = 1;
    // Collision2D coll;

    // Start is called before the first frame update
    void Start()
    {
        speed = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerController>().speed;
        scores = GameObject.Find("score").GetComponent<Text>();
        rigbod = GameObject.FindGameObjectWithTag("Player").GetComponent<Rigidbody2D>();
        grounded = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerController>().grounded;
    }

    // Update is called once per frame
    void Update()
    {
        rigbod.velocity = new Vector2(speed, rigbod.velocity.y);
        deltaRotation = currentRotation - rigbod.transform.eulerAngles.z;
        currentRotation = rigbod.transform.eulerAngles.z;
        if (deltaRotation >= 300) deltaRotation -= 360;
        if (deltaRotation <= -300) deltaRotation += 360;
        WindupRotation += (deltaRotation);
        flips = WindupRotation / 340;
        iflip = (int)flips;
        iflip = iflip * -1;
        flipscore = iflip * 10;
        trickscore = flipscore + rockDestroy;
        scores.text = "score " + (trickscore * multiplier);
        incInScore = trickscore - oldscore;
        if (incInScore >= 10)
        {
            oldscore = trickscore;
        }
        
        //speed += (Mathf.Round(incInScore)) / 100.0f;
      
        if (incInScore > 1 && incInScore <= 10)
        {
            speed = speed + 0.15f;
            counter += 3f;
        }
        if (incInScore > 10 && incInScore <= 20)
        {
            speed = speed + 0.25f;
            counter += 3f;
        }
        if (incInScore > 20 && incInScore <= 50)
        {
            speed = speed + 0.50f;
            counter += 3f;
        }
        if (incInScore > 50 && incInScore <= 100)
        {
            speed = speed + 0.75f;
            counter += 3f;
        }
        if (incInScore > 100 && incInScore <= 200)
        {
            speed = speed + 1f;
            counter += 3.5f;
        }
        if (incInScore > 200)
        {
            speed = speed + 2f;
            counter += 4f;
        }

        if (incInScore > 5 && grounded == false)
        {
            multiplier = multiplier + 1;
        } 
        else if (grounded == true)
        {
            multiplier = 1;
        }

        if (speed > 5.15f)
        {
            speed -= 0.05f * Time.deltaTime;
        } 
        else if (speed == 5.15f)
        {
            speed = 5.15f;
        }
         
        counter -= 1.0f * Time.deltaTime;
 
        if (counter < 0)
        {
            counter = 0;
        }

        if (incInScore >= 10)
        {
            incInScore = 0;             
        }

        if (incInScore < 0)
        {
            incInScore = incInScore * -1;
        }
    }

    private void OnCollisionEnter2D(Collision2D coll)
    {
        //counter = GameObject.FindGameObjectWithTag("Player").GetComponent<tricksScore>().counter;
        if (counter > 0)
        {
            if (coll.collider.tag == "rock")
            {
                Destroy(coll.gameObject);
                speed = speed + 0.15f;
                rockDestroy = rockDestroy + 5;
                counter = counter + 2f;
            }            
        }
    }
}

I know the scripts dirty, but hopefully its still comprehensible for you all. thanks in advance for the help.

Your ground boolean is only set in start, so it will almost always be false. Add the check statement in the update function, as when you set a variable with this property, it will only take the value at that time, it will not automatically update.

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