Damaging an AI
All damage is handled through an interface script called IDamagable. This script acts like a bridge that allows damage calls to be received by any script that uses the IDamageable interface. This is important for custom character controllers as users can then receive the damage call to then damage their character controller's health.
If you’d like to apply damage to an AI directly, for something like a custom character controller, you can do so with the code below. YourTargetReference would be the collision or or target source that you would like to damage.
The damage function through the IDamageable interface script looks like this. DamageAmount is the amount of damage you would like to deal to the referenced target. The AttackerTransform is the transform source of the attacker (such as the player). The RagdollForce is the amount of force that will be applied to the referenced target's ragdoll components on death.
Important: It is very important that the AttackerTransform is the correct source. This should be the transform that holds your player controller's health. Not getting this correct can lead to detection issues.
Assume you get a reference to a target hit with a collision or raycast and it's cached as YourTargetReference. Check to see if IDamageable exists on the target object. If it does, call the damage function.
Last updated