【发布时间】: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