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

Please post any questions about developing your plugin here. Please use the search function before posting!
User avatar
khaimovmr
Member
Posts: 32
Joined: Fri Aug 25, 2017 3:15 am
Contact:

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

Postby khaimovmr » Wed Jun 17, 2020 10:56 am

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.
User avatar
VinciT
Senior Member
Posts: 173
Joined: Thu Dec 18, 2014 2:41 am

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

Postby VinciT » Wed Jun 17, 2020 5:28 pm

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)
ImageImageImageImageImage
User avatar
khaimovmr
Member
Posts: 32
Joined: Fri Aug 25, 2017 3:15 am
Contact:

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

Postby khaimovmr » Wed Jun 17, 2020 7:38 pm

Thanks a lot, VinciT!
player.get_trace_ray() did solve it!

Return to “Plugin Development Support”

Who is online

Users browsing this forum: No registered users and 8 guests