Raycast not colliding with player

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

Hello! I am trying to use a raycast to collide with the player, in order to simulate the enemy noticing the player. For some reason, the raycast works and collides with everything except for my player. How can I get my raycast to detect the player?

The player and enemy are both KinematicBody2D’s and the raycast is set to detect all bodies except for Areas. Here is my code:

if raycast.is_colliding():
	var groups = get_groups()
	if groups.has("player"):
		_player_spotted()

I know the raycast is working because I tested it by using print to print something if it was colliding, and it was, but it didn’t detect the player

:bust_in_silhouette: Reply From: GodotNoob999

Try using is_in_group()

That didn’t work either

CosmicNerd6505 | 2018-04-24 20:58

:bust_in_silhouette: Reply From: kidscancode

What is this script running on? If it’s not the player, then get_groups() is getting the list of groups for the object running the script, not the object the raycast hit.

If you want to query the object the raycast collided with you use get_collider():

if raycast.is_colliding():
    if raycast.get_collider().is_in_group("player"):
        _player_spotted()

The script is attached to the enemy, and the raycast is a child of enemy. I tried the code you suggested, but it didn’t work

CosmicNerd6505 | 2018-04-24 22:57

What does “it didn’t work” mean? Are we supposed to guess? You’ll save a lot of back and forth if you provide more information about what you’re doing. Based on the information you’ve given, my answer should work. However, there are a lot of other factors that can tie into collisions, such as layers, masks, etc.

Here’s another suggestion. Use print() to try and figure out what the raycast is hitting:

if raycast.is_colliding():
    print(raycast.get_collider().name)

kidscancode | 2018-04-24 23:06

Alright, i appreciate the help, but i dont appreciate your condescending attitude. If you don’t have the patience to deal with beginners, then don’t bother at all. I’ll figure it out on my own, thank you very much

CosmicNerd6505 | 2018-04-25 04:12

I’m not being condescending. You literally didn’t include any information about your problem at all. You’ve replied to two answers with nothing but “it didn’t work”. I am sincerely offering advice that you include more information with your questions. I hope you find your answer, one way or another.

kidscancode | 2018-04-25 04:26