0 votes

I need help with getting the node the raycast collided with. Before i used a string that contained the collision id of the node, but the id changes everytime i boot up the project. And the only thing i could do to make the results more reliable would be getting the node name. Is there anyway i can do this?

in Engine by (39 points)

2 Answers

+1 vote
Best answer

You can use get_collider() , it returns the object the ray is colliding with and you can extract it's name using var.name property...

just make sure to add a safe guard in case the ray doesn't collide with anything

by (1,198 points)
selected by

GetCollider() returns Godot.Object which does not contain a definition for Name.

If you are using C#(never used it with Godot) you might need to cast the object to a node or other class to be able to get all the relevant data

0 votes

Use get_collider() which returns the first object that rayCast2D collides with be it an area2D or a body depending on your preferred presets. You can then use to_string() to get the name of the object, ensure to check if the result from get_collider() is not null. You can use this feature to ensures enemies do not see through walls.

by (28 points)
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.