Detect Headshot

hello, i have a question on how to detect headshot, since the character automatically have capsule collider attached when we setup the character controller, so when we add the collider to the character head, it will not be collided to the collider on the character head.
 
Take a look at the demo scene for an example, but there is a second raycast done in order to detect if the hit collider should be the head or the main capsule collider.
 
Hello justin, i have take a look at the demo scene, but i have no idea how to implement the second raycast to detect the head collider because it was inside the capsule collider, can i have an example?
 
You don't have to do it, it's done automatically within the Health component. If you'd like to debug this you can insert a breakpoint on line 347 of Health.OnDamage. This is where the raycast is triggered.
 
okay, here is my code that i put on my character game object, and i make a condition to detect if my shoot hit the Head Collider, but it always log that my shoot hit the Capsule Collider everytime i shoot even i point my gun in the other character head.
 

Attachments

  • code.png
    code.png
    444.9 KB · Views: 7
You should insert a breakpoint within Health.OnDamage to see why the raycast is failing to detect the headshot.
 
code.png

Hello again Justin, finally i can detect the Head collider, but i got another problem to apply the Damage on the player i got the error below.
anyway, i attached the script directly to my player GameObject.

NullReferenceException: Object reference not set to an instance of an object
Opsive.UltimateCharacterController.AddOns.Multiplayer.PhotonPun.Traits.PunHealthMonitor.OnDamage (System.Single amount, UnityEngine.Vector3 position, UnityEngine.Vector3 direction, System.Single forceMagnitude, System.Int32 frames, System.Single radius, Opsive.UltimateCharacterController.Traits.Damage.IDamageOriginator originator, UnityEngine.Collider hitCollider) (at Assets/Plugins/Opsive/UltimateCharacterController/Add-Ons/Multiplayer/PhotonPUN/Scripts/Traits/PunHealthMonitor.cs:55)
Opsive.UltimateCharacterController.Traits.Health.Damage (Opsive.UltimateCharacterController.Traits.Damage.DamageData damageData) (at Assets/Plugins/Opsive/UltimateCharacterController/Scripts/Traits/Health.cs:331)
Opsive.UltimateCharacterController.Traits.Health.Damage (System.Single amount, UnityEngine.Vector3 position, UnityEngine.Vector3 direction, System.Single forceMagnitude, System.Int32 frames, System.Single radius, UnityEngine.GameObject attacker, System.Object attackerObject, UnityEngine.Collider hitCollider) (at Assets/Plugins/Opsive/UltimateCharacterController/Scripts/Traits/Health.cs:310)
Opsive.UltimateCharacterController.Traits.Health.Damage (System.Single amount, UnityEngine.Vector3 position, UnityEngine.Vector3 direction, System.Single forceMagnitude, System.Int32 frames, System.Single radius, UnityEngine.GameObject attacker, UnityEngine.Collider hitCollider) (at Assets/Plugins/Opsive/UltimateCharacterController/Scripts/Traits/Health.cs:291)
Opsive.UltimateCharacterController.Traits.Health.Damage (System.Single amount) (at Assets/Plugins/Opsive/UltimateCharacterController/Scripts/Traits/Health.cs:222)
Headshoot.Damage () (at Assets/Headshoot.cs:30)
Headshoot.OnImpact (System.Single amount, UnityEngine.Vector3 position, UnityEngine.Vector3 forceDirection, UnityEngine.GameObject attacker, System.Object attackerObject, UnityEngine.Collider hitCollider) (at Assets/Headshoot.cs:24)
Opsive.Shared.Events.InvokableAction`6[T1,T2,T3,T4,T5,T6].Invoke (T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6) (at <e8b0cfc00753413a821333bbef3cbca8>:0)
Opsive.Shared.Events.EventHandler.ExecuteEvent[T1,T2,T3,T4,T5,T6] (System.Object obj, System.String eventName, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6) (at <e8b0cfc00753413a821333bbef3cbca8>:0)
Opsive.UltimateCharacterController.Items.Actions.ShootableWeapon.HitscanFire (System.Single strength) (at Assets/Plugins/Opsive/UltimateCharacterController/Scripts/Items/Actions/ShootableWeapon.cs:1077)
Opsive.Shared.Game.SchedulerBase.AddEventInternal[T] (System.Single delay, Opsive.Shared.Game.ScheduledEventBase+InvokeLocation invokeLocation, System.Action`1[T] action, T value) (at <6b6b48297d4f49e9ba244a49e70e6e92>:0)
Opsive.Shared.Game.SchedulerBase.Schedule[T] (System.Single delay, System.Action`1[T] action, T value) (at <6b6b48297d4f49e9ba244a49e70e6e92>:0)
Opsive.UltimateCharacterController.Items.Actions.ShootableWeapon.Fire (System.Single strength) (at Assets/Plugins/Opsive/UltimateCharacterController/Scripts/Items/Actions/ShootableWeapon.cs:846)
Opsive.UltimateCharacterController.Items.Actions.ShootableWeapon.UseItem () (at Assets/Plugins/Opsive/UltimateCharacterController/Scripts/Items/Actions/ShootableWeapon.cs:737)
Opsive.UltimateCharacterController.Character.Abilities.Items.Use.LateUpdate () (at Assets/Plugins/Opsive/UltimateCharacterController/Scripts/Character/Abilities/Items/Use.cs:651)
Opsive.UltimateCharacterController.Character.UltimateCharacterLocomotion.LateUpdateActiveAbilities (Opsive.UltimateCharacterController.Character.Abilities.Ability[] abilities, System.Int32& abilityCount) (at Assets/Plugins/Opsive/UltimateCharacterController/Scripts/Character/UltimateCharacterLocomotion.cs:1064)
Opsive.UltimateCharacterController.Character.UltimateCharacterLocomotion.LateUpdateUltimateLocomotion () (at Assets/Plugins/Opsive/UltimateCharacterController/Scripts/Character/UltimateCharacterLocomotion.cs:1050)
Opsive.UltimateCharacterController.Character.UltimateCharacterLocomotion.UpdatePositionAndRotation () (at Assets/Plugins/Opsive/UltimateCharacterController/Scripts/Character/UltimateCharacterLocomotion.cs:865)
Opsive.UltimateCharacterController.Character.CharacterLocomotion.UpdatePositionAndRotation (System.Boolean fromAnimatorMove) (at Assets/Plugins/Opsive/UltimateCharacterController/Scripts/Character/CharacterLocomotion.cs:527)
Opsive.UltimateCharacterController.Character.CharacterLocomotion.OnAnimatorMove () (at Assets/Plugins/Opsive/UltimateCharacterController/Scripts/Character/CharacterLocomotion.cs:1504)
Opsive.UltimateCharacterController.Character.UltimateCharacterLocomotion.OnAnimatorMove () (at Assets/Plugins/Opsive/UltimateCharacterController/Scripts/Character/UltimateCharacterLocomotion.cs:1793)
 
Top