【问题标题】:how to make an ammo limit decrease from its current value, after it has been increased in C#, Unity?在 C#,Unity 中增加弹药限制后,如何使弹药限制从当前值减少?
【发布时间】:2017-08-09 01:39:27
【问题描述】:

我的角色射箭。她一开始没有零箭,并且在她拿起箭头图标之前不能射任何箭。箭头图标的值为 3。在此之后她可以射箭。该代码工作正常。现在我必须通过 UI 文本显示使这些箭头的值减小。拾取箭头图标时,UI 文本值从 0 变为 3,但在我射箭时它不会减少。我有另一个带有脚本的游戏对象,该脚本将检测何时射箭。发生这种情况时,它会告诉主脚本,“嘿,刚刚射了一箭”。当我射箭时,重点是让文本减少。

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

public class arrowManager : MonoBehaviour {

private Text text;
public static int arrowCount;
public static int arrowRecount;
private int maxArrows = 99;

void Start ()
{
    text = GetComponent<Text> ();
    arrowCount = 0;
}

void Update ()
{
    FullQuiver ();
    arrowCounter ();
}

void arrowCounter()
{
    if (arrowCount < 0) {
        arrowCount = 0;
        text.text = "" + arrowCount;
    }
    if (arrowCount > 0)
        text.text = "" + arrowCount;
}
public static void AddPoints(int pointsToAdd)
{
    arrowCount += pointsToAdd;
}
public static void SubtractPoints(int pointsToSubtract)
{
    arrowCount -= pointsToSubtract;
}

public void FullQuiver()
{
    if (arrowCount >= maxArrows)
    {
        arrowCount = maxArrows;
    }
}
}

带有检测箭头的脚本的游戏对象如下所示。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class arrowDetector : MonoBehaviour {


public int pointsToSubtract;

void OnTriggerEnter2D(Collider2D other)
{
    if (other.gameObject.tag == "arrow")
    {
        arrowManager.SubtractPoints (pointsToSubtract);
    }
}

}

【问题讨论】:

  • 你能在 Trigger 方法中放置一个Debug.Log() 吗?只是看看它是否被调用。
  • 是的,调试一下,我想如果你射箭,你需要使用光线投射来解决你的问题,有时因为你的箭的fps和速度,你会错过你的触发器。检查你的游戏对象也有刚体组件和碰撞器......

标签: c# user-interface unity3d reference static


【解决方案1】:

如果我误解了,请原谅我,但在我看来,您从错误的变量中减去。

由于您正在显示“arrowCount”变量,我想这就是应该减去的。

public static void SubtractPoints(int pointsToSubtract)
{
    if (arrowCount > 0) {

        arrowCount -= pointsToSubtract;//pointsToSubtract is an int value passed to this script from my player script whenever she shoots an arrow.
    }
}

【讨论】:

  • 好吧,它恢复到原来的样子(实际上是你输入的方式)。但是结果是一样的。
【解决方案2】:

在 SubtractPoints 方法中,您正在减损变量“arrowRecount”。 你不想从“arrowCount”中减去吗?如果您使用“arrowCount”,您的文本值应该会正确更新。

【讨论】:

  • 是的,我也以各种方式尝试过,但得到了相同的结果。所以我尝试了一个不同的变量无济于事:(
【解决方案3】:

我想通了。以前,我试图让它在我的播放器脚本中的布尔值变为真时检测我的箭头。那是行不通的,所以我说把它搞砸,只是空了一个检测带有“箭头”标签的游戏​​对象。然后我在这里更新了脚本以反映这一点。昨晚我两天没睡后累死了,所以我忘了在层次结构中输入 pointsToSubtract 的值。感谢大家的回复。

【讨论】:

    猜你喜欢
    • 2017-12-19
    • 1970-01-01
    • 2016-08-17
    • 2017-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-13
    相关资源
    最近更新 更多