当玩家向相反方向移动时,停止减小玩家的距离值?

时间:2017-12-27 16:54:53

标签: c# unity3d

我有一个玩家在遇到平台时会跳起来。有点像涂鸦跳。得分基于y轴行进的距离。问题是,当玩家跳起时,距离值(得分)增加,但是当玩家回到平台上时,距离值减小,因此得分也降低。我想阻止分数下降。这是脚本:

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

2 个答案:

答案 0 :(得分:4)

设置最大高度,如果超过达到的最大高度,则仅更新分数。

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

答案 1 :(得分:0)

void Update () {
     curPos = player.transform.position.y - beginPos;
     if (curPos > 0) {
        distance = Mathf.RoundToInt(curPos * Multiplier);
     }
}