Page 1 of 1

[CSGO] Determine if player's crosshair is on the other player's head

Posted: Wed Jun 17, 2020 10:56 am
by khaimovmr
Hey, guys!
Long time no see.
Does anyone know the way to determine if player is pointing (crosshair) on the other player's head without shooting the weapon?
Previously I've used the prehook of the on_take_damage event, but it's fired only after the weapon is.

Re: [CSGO] Determine if player's crosshair is on the other player's head

Posted: Wed Jun 17, 2020 5:28 pm
by VinciT
Hi khaimovmr, here's a small function you can add to your custom Player class:

Syntax: Select all

# ../headshot_trace/headshot_trace.py

# Source.Python
from commands.client import ClientCommand
from entities.entity import BaseEntity
from players.dictionary import PlayerDictionary
from players.entity import Player


class PlayerHT(Player):
caching = True

def get_head_in_crosshair(self):
"""Returns the index of the player whose head we're aiming at."""
# Fire and store a GameTrace() of the player's current view.
trace = self.get_trace_ray()

# Did the trace hit something?
if trace.did_hit():
# Is that something a player?
if BaseEntity(trace.entity_index).is_player():
# Did the trace hit their head?
if trace.hitbox == 0:
return trace.entity_index

return None


player_instances = PlayerDictionary(PlayerHT)


@ClientCommand('+lookatweapon')
def inspect_weapon(command, index):
player = player_instances[index]

target_index = player.get_head_in_crosshair()
# Make sure we actually found a head.
if target_index is not None:
# Apply 200 damage to the other player.
player_instances[target_index].take_damage(
damage=200, attacker_index=index)

Re: [CSGO] Determine if player's crosshair is on the other player's head

Posted: Wed Jun 17, 2020 7:38 pm
by khaimovmr
Thanks a lot, VinciT!
player.get_trace_ray() did solve it!