【问题标题】:Stop decreasing distance value of player when it moves in the opposite direction?当玩家向相反方向移动时停止减少玩家的距离值?
【发布时间】:2017-12-28 01:11:23
【问题描述】:

我有一个玩家在碰到平台时会跳跃。有点像涂鸦跳跃。分数基于在 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);
 }

【问题讨论】:

  • 猜猜你可以给玩家添加一个属性,表明他是否在跳跃,如果是,不扣分?
  • 听起来您可能需要一个额外的变量,例如 apex 并围绕它构建您的计算。
  • 玩家不断跳跃!没有动静。他触到平台,朝+y方向跳了起来。他错过了平台,游戏结束了。还有其他想法吗?我尝试创建另一个变量 maxDist 来存储距离并将其与之前的值进行比较,但我无法使其工作。
  • 有一个lastPos 的变量并且只更新lastPos 并计算curPos > lastPos 的分数?
  • 我应该将 lastPos 等同于什么?

标签: c# unity3d


【解决方案1】:

设置最大高度,并且仅在超过达到的最大高度时更新分数。

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

【讨论】:

  • 随着玩家不断向上移动,无法设置 maxHeight!它的位置不断变化。
  • 我想你误会了。在这种情况下,maxHeight 只是记住玩家曾经达到的最高点。如果当前玩家位置超过maxHeight,那么您知道他比以前高,因此会更新分数并将maxHeight 设置为当前位置。
  • 工作!工作!太感谢了!使用另一个变量是要走的路。再次感谢!!!!
【解决方案2】:
void Update () {
     curPos = player.transform.position.y - beginPos;
     if (curPos > 0) {
        distance = Mathf.RoundToInt(curPos * Multiplier);
     }
}

【讨论】:

  • 不起作用。价值还在下降。作为参考,玩家不会停留在 (0,0,0)。他不断向上移动。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-04-18
  • 2022-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-30
  • 1970-01-01
相关资源
最近更新 更多