Stop decreasing distance value of player when it moves in the opposite direction?

c# unity3d

86 观看

2回复

31 作者的声誉

I have a player that jumps when it hits a platform. Kind of like doodle jump. The score is based on the distance travelled in the y axis. The problem is, when the player jumps up, the distance value (score) increases but when the player lands back onto the platform, the distance value decreases and hence the score also decreases. I want to stop the score from decreasing. Here's the script :

public GameObject player;
float beginPos;
float curPos;
public int Multiplier;

void Start () {
     beginPos = player.transform.position.y;
}

 void Update () {
     curPos = player.transform.position.y - beginPos;
     int Distance = Mathf.RoundToInt(curPos * Multiplier);
     Debug.log(Distance);
 }
作者: Vivek Negi 的来源 发布者: 2017 年 12 月 27 日

回应 2


0

488 作者的声誉

void Update () {
     curPos = player.transform.position.y - beginPos;
     if (curPos > 0) {
        distance = Mathf.RoundToInt(curPos * Multiplier);
     }
}
作者: Milso 发布者: 2017 年 12 月 27 日

4

74 作者的声誉

决定

Set a max height and only update score if you go past the max height reached.

float beginPos;
float curPos;
public int Multiplier;
float maxHeight;
 void Start () {
     beginPos = player.transform.position.y;
     maxHeight = beginPos;
}
void Update () {

    curPos = player.transform.position.y - beginPos;

     if(curPos > maxHeight)
     {
         int Distance = Mathf.RoundToInt(curPos* Multiplier);
         maxHeight = curPos;
         Debug.log(Distance);
     }
}
作者: Dalton 发布者: 2017 年 12 月 27 日
32x32