How to tell if one player is in line of fire of another one?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By sxkod

Hi all
I am trying to find out if an AI agent is in the line of fire of player so it can try and evade. I wrote the following. It seems to work but not well. It does recognize if the agent is indeed in the line of fire of the player. However if also seem to claim collisions even when the player is patently not rotated / facing the AI, such as at approx 45-60 angles too. Am I missing something?

Thanks

func _qry_lof():
qryres["lof"]=null
var plys=get_tree().get_nodes_in_group("ply")
if plys.size()<=0:return 0

for n in plys:
	var gto=plys[0].global_transform
	var collided=space.intersect_ray(gto.origin, gto.basis.z*5, [get_parent().get_node("floor/floorbody")])
	if collided:
		print("ouch ",collided)
		qryres["lof"]=collided
		return 1
return 0

Also the player uses the following code to move : courtesy of Jeremy Bullock and Jayanam on youtube :-). Could it be that in using set_rotation, I am messing up the global transform? If so what are my options? Thanks for all the help.

#movement
func walk(delta):
	direction = Vector3()
	var is_moving=false
	var aim = get_parent().get_node("Camera").get_global_transform().basis
	if Input.is_key_pressed(KEY_UP):
		direction -= aim.z
		is_moving=true
	if Input.is_key_pressed(KEY_DOWN):
		direction += aim.z
		is_moving=true
	if Input.is_key_pressed(KEY_LEFT):
		direction -= aim.x
		is_moving=true
	if Input.is_key_pressed(KEY_RIGHT):
		direction += aim.x
		is_moving=true
	direction.y = 0
	direction = direction.normalized()
	velocity.y += gravity * delta	
	var temp_velocity = velocity
	temp_velocity.y = 0
	var target = direction * MAX_SPEED	
	temp_velocity = temp_velocity.linear_interpolate(target, ACCEL * delta)
	velocity.x = temp_velocity.x
	velocity.z = temp_velocity.z
	velocity = move_and_slide(velocity, Vector3(0, 1, 0))

	if is_moving:
		var angle=atan2(temp_velocity.x,temp_velocity.z)
		var char_rot=get_rotation()
		char_rot.y=angle
		set_rotation(char_rot)

Not to answer my own question but the following change seem to help.

if is_moving:
	var angle=atan2(temp_velocity.x,temp_velocity.z)
	var char_rot=get_rotation()
	#char_rot.y=angle
	#set_rotation(char_rot)
	global_rotate(Vector3(0,1,0),angle-char_rot.y)

sxkod | 2019-05-30 19:07