Juan Francisco Patino
Juan Francisco Patino

Reputation: 83

Object not being destroyed when shot with a bullet - UNITY3D C#

So in my game, there's a gun that sprays bullets, and I'm trying to make a gameObject destroy on collision with the bullets. The bullets are based off of one gameObject (Capsule). I've tried these two scripts so far:

using UnityEngine;
using System.Collections;

public class whenshot : MonoBehaviour {

void OnCollisionEnter(Collision col)
{
    if (col.gameObject.name == "Bullet")
    {
        Destroy(col.gameObject);
    }
}
}

and:

using UnityEngine;
using System.Collections;

public class whenshot : MonoBehaviour {


void OnCollisionEnter(Collision col)
{
    if (col.gameObject.name == "Bullet")
    {
        Destroy(this); //the difference between the two is that I changed "col.gameObject" to "this"
    }
}
}

I'm shooting the object but it's not disappearing/destroying itself. How can I fix this?

Here's a visual if it helps: Visual

Upvotes: 1

Views: 2484

Answers (4)

aaa
aaa

Reputation: 96

I can't comment so I will make an answer:

You can make the bullet go fast, just set the collision detection to continious dynamic.

It has an almost %100 success rate.

Upvotes: 0

Juan Francisco Patino
Juan Francisco Patino

Reputation: 83

Ok so I figured it out, it's kind of weird but apparently I was making the bullets move too fast... I had to slow down the "Bullet_Forward_Force" float to about 150f to make it work. Thanks to everyone who answered though.

Upvotes: 0

karnage
karnage

Reputation: 183

this refers to the object instance of the caller (this is basic OOP), i.e., whenshot, and not gameObject. So the second sample is effectively Destroying the instance of the script from the gameObject it is attached to.

The first script is technically fine, and should work, provided these conditions are met:

  1. Either the projectile (bullet) or the target (or both) have a non-kinematic rigidbody component attached. (Unity docs.)
  2. Both have 3D Collider components.
  3. The name of every single bullet gameObject that collides with the target is exactly "Bullet".
  4. All projectile objects have this script as a component.

Some suggestions

Use prefabs and tags: take your bullet primitive and store it as a prefab. Add a tag to the prefab called "Bullet". Do the same for the target and tag it as "Target". Tag the player as "Player". In the "gunController", set a reference to the bullet prefab and make it Instantiate bullets on whatever trigger you're using. In the bullet's script, use CompareTag("Target") instead of == and Destroy both the target gameObject and this.gameObject.

It seems to me that the above is the behaviour you want. If that is the case, there is no delay between collision and destruction, and hence no need to simulate physics whatsoever. Unless you have some other physics interactions with bullets/targets, mark the one without a rigidbody as a Trigger.

A Strong Suggestion

Go through Unity tutorials.

Upvotes: 4

K0D3R
K0D3R

Reputation: 148

This is an example from a 2D Game I made a while back, but i think it might help.

void OnTriggerEnter2D(Collider2D other)
{
    if (other.tag == "Destroyable")
    {
        Destroy(other.gameObject);
    }
}

I used this to destroy certain blocks when the player would shoot them so many times, just switch them to the 3D Collider and Trigger, but it should do the trick for ya (i hope ^^).

edit: this script should be attached to your bullet prefab

Upvotes: 0

Related Questions