Bullet impact on enemy

Hi

I have a slight issue here. When i fire a bullet at the enemy, it does not react with it. But suddenly it reacts with it, but then the enemy gets thrown away by the enormous force. How can I make the bullet just colliding with it, and what is the best way to make a collider detecting function that will not let you down when needed?

Set “Is Trigger” to true in the collider component so it will trigger the collision but doesn’t apply physics collision (you will detect collisions using the function OnTriggerEnter through a script attached to the collider). Also, since a bullet is usually small and flies at high velocity be sure to set “Collision Detection” to “Continuous” on the bullet’s RigidBody

Usually you should use Raycast for fast bullets, and instantiate only slow projectiles like rockets or missiles. Fast rigidbodies may “pass through” the target, because they can be before the target in one physics cycle and after it in the next. To do a raycast shot, you should do the following (weapon script):

var shotSound: AudioClip; // drag the shot sound here
var impact: float = 20; // define the impact force

function Fire(){
  audio.PlayOneShot(shotSound); // play the shot sound
  var hit: RaycastHit;
  // if something hit, apply the damage and the impact (only if it's a rigidbody):
  if (Physics.Raycast(transform.position, transform.forward, hit)){
    if (hit.rigidbody){ // apply impact if the target is a rigidbody:
      hit.rigidbody.AddForce(transform.forward * impact);
    }
    // apply damage (ApplyDamage is an enemy function):
    hit.transform.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
  }
}

The enemy script must have a ApplyDamage to reduce its health - something like this:

var health: float = 100;

function ApplyDamage(damage: float){
  health -= damage;
  if (health <= 0){
    // enemy is dead: play a death scream, explode it, destroy it, etc.
  }
}

To shoot, just call the Fire function. It’s a common practice to call it using BroadcastMessage(“Fire”), thus you may have different weapons, each one with its own Fire function, and only the currently active weapon will effectively fire.

NOTE: Just to explain the big force mistery: you probably let the bullet with the default mass = 1, and perhaps the enemy too - the reaction would be like a 60Kg bullet hitting a 60Kg enemy! You should tweak the masses to have a more realistic reaction with instantiated bullets - say, 0.1 for the bullet and 10 for the enemy. This obviously isn’t needed with raycast shooting, since there’s no instantiated bullet - you just have to adjust the impact force.