Collider of bullet

Hi Please help me ,Im making a 2d shooting game,the problem is this

function goFire () {

var bullet = Instantiate(bulletGun , mainPoint.transform.position, Quaternion.identity);
bullet.GetComponent.().velocity = Vector3(22,yBullet,0);

}

my problem is i want to destroy the bullet which is I Instantiate in this function when the bullet collide or hit to the Object I shooting at.

please help…

I'm assuming you have some function to detect if the bullet hit something? void OnCollisionEnter2D(Collision2D coll) { } Depending on where this piece of code is, you can actually just find the bullet by tag or if it is a prefab with a script controlling it, you can just destroy it with Destroy(this.gameObject)

Nevermind, I am wrong. One second let me try to wrap my head around this. I completely understand what you are trying to do, the above script simply slows the character down over X time.

1 Answer

1

The problem with your script is that, this function is only called when the shoot button is pressed, am I correct?
If not, you could use raycasts to detect collisions on a fixed update.

If it is a rigidbody, you need to set the rigidbody collision detection to continous dynamic.

Next, you put a script on the bullet with the method OnCollisionEnter2D.
Then follow it by the Destroy method. (And whatever else there is to do).

So, you could attach a script on the bullet (then you wouldn’t need to call any methods or classes).

Example Rigidbody collision code:

    void OnCollisionEnter(Collision collision)
    {
        float damage = 50;
        collision.gameObject.SendMessage("Damage", damage, SendMessageOptions.DontRequireReceiver);
        Destroy(gameObject);
    }