[英]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.