0 votes

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

in Engine by (22 points)

2 Answers

0 votes

Try using is_in_group()

by (74 points)
edited by

That didn't work either

0 votes

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()
by (20,355 points)

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

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)

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

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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.