+1 vote

I am trying to raycast from the ememy(pos) to the player(targetPos) and have gotten: Invalid get index 'collider' (on base: 'Dictionary').

Code:

var space = get_world().direct_space_state
var collision = space.intersect_ray(pos, targetPos, [self])
print(collision)
if collision != null and collision.collider.is_in_group("Player"):
    runActive = true
Godot version 3.5.1
in Engine by (16 points)
edited by

Edited to fix code formatting. Use the {} button in future posts to format code blocks.

1 Answer

0 votes
Best answer

I think your problem is here:

if collision != null and collision.collider.is_in_group("Player"):

As documented intersect_ray() will return an EMPTY dictionary if no ray intersection is found. So, I assume that's what's happening here. However, an EMPTY dictionary IS NOT null, so it'll get through that first != null check without issue. Then, the very next check attempts to access the collider key, which won't exist and triggers the error you report.

Change the above code to:

if collision and collision.collider.is_in_group("Player"):
by (19,268 points)
selected by

Thanks! I had tried this in many different ways but not if it was colliding and was hitting the player.

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 Frequently asked questions and 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 [email protected] with your username.