Topic was automatically imported from the old Question2Answer platform.
Asked By
FreddieBees
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
Edited to fix code formatting. Use the {} button in future posts to format code blocks.
if collision != null and collision.collider.is_in_group("Player"):
As documentedintersect_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"):
Thanks! I had tried this in many different ways but not if it was colliding and was hitting the player.