[英]Detecting Collision Between Clones Unity
是否可以識別克隆之間的沖突。 我需要發射一個彈丸作為克隆體,並在它接觸到物體時將其摧毀。 無論如何使用標簽和void OnCollisionEnter()
函數。 這是我的代碼:
if (Input.GetKeyDown(KeyCode.Mouse1))
{
shootProjectile();
}
void shootProjectile()
{
var ForwardDirection = cameraTarget.transform.forward;
var RightDirection = cameraTarget.transform.right;
var UpDirection = cameraTarget.transform.up;
GameObject clone = Instantiate(Projectile, transform.position + 1 * ForwardDirection, transform.rotation);
clone.GetComponent<Rigidbody>().AddForce(ForwardDirection * 600);
clone.GetComponent<Rigidbody>().AddForce(UpDirection * 200);
clone.GetComponent<Rigidbody>().AddTorque(RightDirection * 200);
Destroy(clone, 3.0f); // Destroy the clone after 5 seconds
}
您可以將標簽分配給預制件。 但是,如果您只想在彈丸擊中任何東西時將其摧毀。 那么你不需要標簽。
void OnCollisionEnter(Collision collision)
{
Destroy(gameObject)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.