Search code examples
c#visual-studiounity-game-engineclonecollision

Detecting Collision Between Clones Unity


Is it possible to identify a collision between a "clone"? I must launch a projectile as a clone and destroy it when it touches an object. Is there any way to use tags and the void OnCollisionEnter() function? Here is my code:

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
}

Solution

  • You can assign a tag to a prefab. But if you just want to destroy the projectile when it hits anything. then you don't need a tag.

     void OnCollisionEnter(Collision collision)
        {
            Destroy(gameObject)
    
        }