raycast的Unity2D问题

时间:2019-06-25 01:05:24

标签: c# unity3d

我正在做一个3场比赛,并且使用Raycast打破糖果,但是当我尝试显示玩家摔坏了多少糖果时,它的显示比预期少了。

private List<GameObject> FindMatch(Vector2 direction)
    {
        List<GameObject> matchingCandies = new List<GameObject>();
        RaycastHit2D hit = Physics2D.Raycast(this.transform.position, direction);
        while(hit.collider != null && hit.collider.GetComponent<SpriteRenderer>().sprite == spriteRenderer.sprite) {
            matchingCandies.Add(hit.collider.gameObject);
            hit = Physics2D.Raycast(hit.collider.transform.position, direction);
        }

        return matchingCandies;
    }

    private bool ClearMatch(Vector2[] directions)
    {
        List<GameObject> matchingCandies = new List<GameObject>();

        foreach(Vector2 direction in directions) {
            matchingCandies.AddRange(FindMatch(direction));
        }
        if(matchingCandies.Count >= BoardManager.MinCandiesToMatch) {
            foreach(GameObject candy in matchingCandies){
                goalManager.CompareGoals(candy.GetComponent<SpriteRenderer>().sprite.ToString());
                goalManager.UpdateGoals();
    }
public class BlankGoal
{
    public int numberNeeded;
    public int numberCollected;
}

public class GoalManager : MonoBehaviour
{
    public BlankGoal[] levelGoals;

public void UpdateGoals()
    {
        for(int i = 0; i < levelGoals.Length; i++)
        {
            currentGoals[i].thisText.text = "" + levelGoals[i].numberCollected + "/" + levelGoals[i].numberNeeded;
            if(levelGoals[i].numberCollected >= levelGoals[i].numberNeeded)
            {
                goalsCompleted++;
                currentGoals[i].thisText.text = "" + levelGoals[i].numberNeeded + "/" + levelGoals[i].numberNeeded;
            }
        }
    }

    public void CompareGoals(string goalToCompare)
    { 
        for(int i = 0; i < levelGoals.Length; i++)
        {
            if(goalToCompare == levelGoals[i].matchValue)
            {
                levelGoals[i].numberCollected++;
            }
        }
    }

当我打破3个糖果时,Collect的数字为2,如果打破4为3,依此类推

0 个答案:

没有答案