我的角色射箭。她開始沒有零箭頭,直到她拿起一個箭頭圖標才能開槍。箭頭圖標的值爲3.在此之後,她可以射箭。該代碼工作正常。現在我必須通過UI文本顯示來減少這些箭頭的數值。當拾取箭頭圖標時,UI文本值從0更改爲3,但在拍攝箭頭時它不會減少。我有另一個帶有腳本的遊戲對象,該腳本可以檢測何時拍攝箭頭。當這種情況發生時,它會告訴主要腳本:「嘿,箭頭剛剛被擊中。」當我射箭時,重點是讓文字減少。在C#中增加Unity後,如何使彈藥限制從當前值降低?
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);
}
}
}
你能放置一個'的debug.log()'觸發裏面的方法?只是爲了看看它是否被稱爲。 –
是,調試它,我認爲,如果你開除你的箭,你需要使用光線投射來解決問題,有時會因爲你的箭頭的FPS和速度,你會想念你的觸發器。檢查你的gameobjects具有剛體組件也和對撞機... – BlackMB