Enemy Doesn't Destroy When In Contact With Bullet

I am making a first person shooter where you shoot robot enemies. For some reason when the bullet comes in contact with the robot, the robot launches back.

This is the destroy robot script:

function OnTriggerEnter (other : Collider)
{
	if(other.tag == "Bullet")
	{
		Destroy(gameObject);
	}	
}

I believe the problem is the bullets. I am using the cod style script at http://forum.unity3d.com/threads/cod-modern-warfare-style-gun-script.52651/

This is the bullet script:

private bool isTracer = false;          // used in raycast bullet system... sets the bullet to just act like a tracer

    private string[] bulletInfo = new string[2]; // damage and owner name sent to hit object

    public GameObject Owner  // sets and returns the bullet owners gameObject
    {
        get { return owner; }
        set { owner = value; }
    }

    public Collider ColliderToIgnore  // not really used at the moment, can send a collider to the bullet if you want it to ignore it
    {
        get { return ignore; }
        set { ignore = value; }
    }

    public void SetUp(float[] info) // information sent from gun to bullet to change bullet properties
    {       
        damage = info[0];              // bullet damage
        impactForce = info[1];         // force applied to rigid bodies
        maxHits = info[2];             // max number of bullet impacts before bullet is destroyed
        maxInaccuracy = info[3];       // max inaccuracy of the bullet
        variableInaccuracy = info[4];  // current inaccuracy... mostly for machine guns that lose accuracy over time
        speed = info[5];               // bullet speed
        // drection bullet is traveling
        direction = transform.TransformDirection(Random.Range(-maxInaccuracy, maxInaccuracy) * variableInaccuracy, Random.Range(-maxInaccuracy, maxInaccuracy) * variableInaccuracy, 1);

        newPos = transform.position;   // bullet's new position
        oldPos = newPos;               // bullet's old position
        velocity = speed * transform.forward; // bullet's velocity determined by direction and bullet speed

        // schedule for destruction if bullet never hits anything
        Destroy(gameObject, lifetime);
    }

   // Update is called once per frame
    void Update()
    {
       if (hasHit)
            return; // if bullet has already hit its max hits... exit

        // assume we move all the way
        newPos += (velocity + direction) * Time.deltaTime;

        // Check if we hit anything on the way
        Vector3 dir = newPos - oldPos;
        float dist = dir.magnitude;

        if (dist > 0)
        {
            // normalize
            dir /= dist;

            RaycastHit[] hits = Physics.RaycastAll(oldPos, dir, dist);        
            

            // Find the first valid hit
            for (int i = 0; i < hits.Length; i++)
            {
                RaycastHit hit = hits*;*

//Debug.Log( "Bullet hit " + hit.collider.gameObject.name + " at " + hit.point.ToString() );

if (ShouldIgnoreHit(hit))
continue;

// adjust new position
newPos = hit.point;

// notify hit
OnHit(hit);
// Debug.Log(“if " + hitCount + " > " + maxHits + " then destroy bullet…”);
if (hitCount >= maxHits)
{
hasHit = true;
Destroy(gameObject);
break;
}
}
}

//=========================================================================================
// test for hit against a back of a wall
// want to try to have bullet holes on both sides of a penetrated wall
//=========================================================================================
RaycastHit hit2;
if (Physics.Raycast(newPos, -dir, out hit2, dist)) // send a ray behind the bullet to check for exit impact
{ // testing to see if the bullet passed through something
//RaycastHit hit2 = hits2[j];

if ((!hasHit))// && (hit2.transform != owner.transform))
{
OnBackHit(hit2); // send rear impact and check what to do with it
}
}
//============================================================================================

oldPos = transform.position; // set old position to current position
transform.position = newPos; // set current position to the new position
}

protected virtual bool ShouldIgnoreHit(RaycastHit hit)
{
if (hit.collider == this.collider)
return true; // if I hit myself… ignore it

return false;
}

protected virtual void OnHit(RaycastHit hit)
{
hitCount++; // add another hit to counter
Vector3 contact = hit.point; // point where bullet hit
Quaternion rotation = Quaternion.FromToRotation(Vector3.up, hit.normal); // rotation of bullet impact

switch (hit.transform.tag) // decide what the bullet collided with and what to do with it
{
case “bullet”:
// do nothing if 2 bullets collide
break;
case “Player”:
// add blood effect
break;
case “wood”:
// add wood impact effects
break;
case “stone”:
// add stone impact effect
break;
case “ground”:
// add dirt or ground impact effect
break;
default: // default impact effect and bullet hole
// create an impact effect
Instantiate(impactEffect, hit.point + 0.1f * hit.normal, Quaternion.FromToRotation(Vector3.up, hit.normal));
// create a bullet hole
GameObject newBulletHole = Instantiate(bulletHole, contact, rotation) as GameObject;
// attach the bullet hole to the hit object, this keeps holes attached to rigid bodies as they move about
newBulletHole.transform.parent = hit.transform;
break;
}

if (!isTracer) // if this is a bullet and not a tracer, then apply damage to the hit object
{
// send a message to the hit object… let it know it was hit
bulletInfo[0] = ownersName; // tell hit object who hit them
bulletInfo1 = damage.ToString(); // tell them how much damage they recieved
// send the message
hit.collider.SendMessageUpwards(“ImHit”, bulletInfo, SendMessageOptions.DontRequireReceiver);

if (hit.rigidbody) // if we hit a rigi body… apply a force
{
hit.rigidbody.AddForce(transform.forward * impactForce, ForceMode.Impulse);
}
}
}

protected virtual void OnBackHit(RaycastHit hit)
{

Vector3 contact = hit.point;
Quaternion rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);

switch (hit.transform.tag)
{
case “bullet”:
// do nothing if 2 bullets collide
break;
case “Player”:
// add blood effect
break;
case “wood”:
// add wood impact effects
break;
case “stone”:
// add
break;
case “ground”:
// add
break;
default:
Instantiate(impactEffect, hit.point + 0.1f * hit.normal, Quaternion.FromToRotation(Vector3.up, hit.normal));
GameObject aBulletHole = Instantiate(bulletHole, contact, rotation) as GameObject;
aBulletHole.transform.parent = hit.transform;
break;
}
}

public void setPlayer(string pName)
{
ownersName = pName; // player that shot fired this bullet… used for multiplayer (not fully working yet)
}

public void SetTracer()
{
isTracer = true; // tell this bullet it is only a tracer… keeps this object from applying damage
}
}
I know it’s a long one. Anyway the bullet does have a rigid body and so does the robot. Also the robots trigger is set. I am a sorta beginner in unity and this problem has really stumped me. Ask any questions you need to and I’ll try my best to answer them.

You said your robot has a rigidbody. So the reason why it gets “launched back” when hit by a bullet is because of Lines 152-154:

if (hit.rigidbody) // if we hit a rigi body... apply a force
{
    hit.rigidbody.AddForce(transform.forward * impactForce, ForceMode.Impulse);
}

As for why your robot’s “Destroy” script is not being called, I’m guessing that’s because your bullet’s collider is not set to IsTrigger. So, OnTriggerEnter won’t be called - you want to use OnCollisionEnter instead.

Dude!!! @brickboy700
it might be late but if you look closer a little bit , you’ll get that every bullet you shoot to GameObject gets parent of that GameObject and i searched a little about detecting Childs then i found
THIS :

function Update () {

if(transform.childCount > 0) {
 
 Destroy(this.gameObject);
 }

AND WORKED
just put the script on ur robot or zombie

now you can set a variable for counting the childs as health like this :

var hit : float; //for example if hitted 5 times then DESTROYY

function Update () {

if(transform.childCount > hit) {
 
 Destroy(this.gameObject);
 }

i don’t know coding btw but it feels good when i discover something lol
Although this is super simple and im super noob in coding :smiley:
let me know what u did

Cheers!