简体   繁体   中英

how to implement a score combo system in endless runner gamme

this is the 3rd time asking this question and ill try to be more clear this time. I am making an endless skiing game in unity and want to make a combo/score multiplier system (similar to as seen in ski safari & alto). Basically, i want the combo counter/multiplier to increase for each trick performed while not touching the ground and then multiply it with the score of the trick performed. But in my game after the player performs a trick, the multiplier keeps increasing endlessly instead of just increasing by 1. Please check my code and help me fix this.

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;
private int displayScore;
// private bool isScore5 = false;
//private int timesScoreInc = 0;
// 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()
{
    grounded = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerController>().grounded;
    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) * multiplier;
    displayScore = displayScore + trickscore;
    scores.text = "score " + displayScore;
    incInScore = trickscore - oldscore;
    /* if (incInScore >= 5)
     {
         isScore5 = true;
     }*/

    if (incInScore >= 5)
    {
        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 ( grounded == false)
     {
        multiplier = timesScoreInc + 1;
     }*/

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

    if (grounded == true)
    {
        multiplier = 1;
    }
    Debug.Log(multiplier);

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

  
    oldscore = 0;
    trickscore = 0;
    flips = 0;
    flipscore = 0;
    iflip = 0;
    rockDestroy = 0;
    incInScore = 0;
      


    if (incInScore < 0)
    {
        incInScore = incInScore * -1;
    }
}
private void OnCollisionEnter2D(Collision2D coll)
{

    if (counter > 0)
    {
        if (coll.collider.tag == "rock")
        {
            Destroy(coll.gameObject);
            speed = speed + 0.15f;
            rockDestroy = (rockDestroy + 5);
            counter = counter + 2f;
           
        }

    }
}


}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class tricksScore : MonoBehaviour{
    private GameObject player;//get the player in start so you don't have to look for it during the update

    private float deltaRotation = 5;//give the player a few degrees
    private Quaternion oldRotation;//use quaternions so you don't have to account for the euler overflow
    public static Rigidbody2D rigbod;
    public Text scores;
    private int trickscore;
    private int iflip;
    private int incInScore;
    public float speed;
    private float counter;
    private int flipscore;
    private int rockDestroy;
    private bool grounded;
    private int multiplier = 1;
    private int displayScore;


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

    // Update is called once per frame

    void Update(){
        grounded = player.GetComponent<PlayerController> ().grounded;
        speed = player.GetComponent<PlayerController> ().speed;
        rigbod.velocity = new Vector2 (speed, rigbod.velocity.y);
        deltaRotation += Mathf.Abs (Quaternion.Angle (rigbod.transform.rotation, oldRotation));//accumulate the absolute value of the current rotation
        oldRotation = rigbod.transform.rotation;//save current rotation as old
        iflip = Mathf.FloorToInt (deltaRotation / 360);//check to see how many flips have been done
        deltaRotation -= iflip * 360;//subtract the flips from the current rotation so they're only counted once
        flipscore = (iflip * 10);
        trickscore = (flipscore + rockDestroy) * multiplier;
        rockDestroy = 0;//have to reset this so its only counted once
        displayScore = displayScore + trickscore;
        scores.text = "score " + displayScore;

        incInScore += trickscore;

        if (trickscore>0){//only increase speed/multiplier if a trick has been performed
            if (incInScore > 1 && incInScore <= 10) {
                speed += 0.15f;
                counter += 3f;
            }
            if (incInScore > 10 && incInScore <= 20) {
                speed += 0.25f;
                counter += 3f;
            }
            if (incInScore > 20 && incInScore <= 50) {
                speed += 0.50f;
                counter += 3f;
            }
            if (incInScore > 50 && incInScore <= 100) {
                speed += 0.75f;
                counter += 3f;
            }
            if (incInScore > 100 && incInScore <= 200) {
                speed += 1f;
                counter += 3.5f;
            }
            if (incInScore > 200) {
                speed += 2f;
                counter += 4f;
            }

            if (incInScore >= 5)
                multiplier++;
        }
        
        if (grounded){
            incInScore = 0;
            multiplier = 1;
            deltaRotation=5;
            oldRotation = rigbod.transform.rotation;
        }

        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;

        player.GetComponent<PlayerController> ().speed = speed;//update the players speed

    }

    private void OnCollisionEnter2D(Collision2D coll){
        if (counter > 0){
            if (coll.collider.tag == "rock"){
                Destroy(coll.gameObject);
                player.GetComponent<PlayerController> ().speed += 0.15f;//be sure to update players speed
                rockDestroy +=5;
                counter += 2f;
            }
        }
    }
}

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