Home > Software engineering >  Detecting Collision Between Clones Unity
Detecting Collision Between Clones Unity

Time:07-24

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
}

CodePudding user response:

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)

    }
  • Related