簡體   English   中英

如何在if語句中檢查沖突

[英]How to check for collision in if-statement

我有買瓷磚的方法,我需要檢查它是否與玩家擁有的另一瓷磚相撞。

我知道func OnCollisionEnter等,但是我認為我需要其他方式並在if語句中進行檢查。

void BuyTile()
{
    if (_selected.tag == "Free") // if no owners
    {
        _selected.GetComponent<Renderer>().material.color = Color.green;
        _selected.tag = "Player1";
        pc.CountTiles();
    }
    _selected = null;
}

我需要這樣的東西,如果(_selected.tag == "Free" && _selected.CollideWithGO.tag == "Player1")但是idk如何不使用func來做到這一點

跟蹤碰撞:

public class CollisionTracker : MonoBehaviour
{
    Collision collision;

    void Update()
    {
        if (collision != null && collision.gameObject.tag == "Player")
        {
            Debug.Log("Colliding with Player!");
        }
    }

    void OnCollisionEnter(Collision collision)
    {
        this.collision = collision;
    }

    void OnCollisionExit(Collision collision)
    {
        this.collision = null;
    }
}

ps:解決

bool Neighboor(GameObject centerTarget)
{
    bool GotTrue = false;
    Collider[] hitColliders = Physics.OverlapSphere(centerTarget.transform.position, 1f);
    for (int i = 0; i<hitColliders.Length; i++)
    {
        if(hitColliders[i].tag == "Player1")
        {
            GotTrue = true;
            break;
        }
    }
    if (GotTrue)
        return true;
    else return false;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM