Gagan Ahuja
Gagan Ahuja

Reputation: 53

Bullet not always hitting Enemy

void OnCollisionEnter(Collision col){

        if (col.gameObject.name == "Enemy1") {
                    enemyDamage++;

            GameObject clone = (GameObject) Instantiate (tempBloodSplat,enemyObj.position,enemyObj.rotation);
                    Destroy (clone , 0.5f);
            if (enemyDamage > 3) {

                anim.SetFloat ("Die", 0.5f);
                Destroy (enemyObj.gameObject , 5.0f);
            }
            Debug.Log ("Bullet is hitting Enemy");
        }
    }

This is my code, i used on BulletObject

  1. My Bullet Object has Collider
  2. My Enemy has a Collider
  3. My Enemy has a Rigidbody
  4. Bullet is not having Rigidbody

I have problem that when i shoot, the bullet is hitting the Enemy in his range, like the circle under the body of the Enemy,Image

Sometimes the bullet hits correctly (means enemy is damaged), but sometimes the bullet moves out without making any damage to the enemy,I don't know why its happening.Does the velocity of the bullet has any effect on it..

Please help or guide me to solve this problem, Thanks

Upvotes: 1

Views: 711

Answers (1)

Gustavo Guimaraes
Gustavo Guimaraes

Reputation: 72

If you are not using a rigidbody on the bullet, then you are probably updating the bullets position vector directly, and what could be happening is the following: enter image description here. Since the bullet is not a rigidbody, Unity does not 'assume' it should behave like one and thus does not do an actual physics simulation of the bullet movement (which would probably include a raycast from start position to end position and colision checking in between). If you have a problem with adding a rigidbody to the bullet, then do the raycast yourself. You will even learn a bit of how the physics simulation behind unity actually might work!

Good Luck!

Upvotes: 2

Related Questions