This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
+1 vote

Hi everyone

I'm quite new to Godot so apologies if this is a silly question, but I wasn't able to find a solution online.
I'm trying to rotate a RayCast2D and once it's rotated, check if it collides with something. The player can rotate the character and attack using the same key, e.g. pressing "W" will result in the character looking up and attacking up as well. The issue I'm facing at the moment is that it seems like the collision is checked before the rotation happens. I'm using the following two lines of code to do this:

attackRay.rotation_degrees = rad2deg(atan2(facingDir.y, facingDir.x))

where attackRay is the RayCast2D and facingDir is the direction the character should be turning to, determined by the user input. If there is an object above the player and I press "W", the raycast is rotated perfectly fine but the console prints "False". If I press it again, it prints "True". If I then press e.g. "S" the raycast points downwards but the console output is "True" again, although there is no object to collide with. Pressing "S" again outpus "False".
The point is, apparently checking for the collision happens prior to the rotation even though in the script rotating the raycast is done before.
I've been trying to fix this for quite some time now but can't seem to get it to work. Any input would be highly appreciated.

in Engine by (13 points)

Just call force_raycast_update() on the raycast.

Thank you very much!

2 Answers

+2 votes

Don't worry, we all have silly questions (tought i do not ask them, don't be like me).
Anyway, here, what you have to do is update the raycast by calling force_raycast_update()on it.

by (56 points)
+2 votes

i think you have to force an update

#adjust rotation etc. here
if raycaster.is_colliding():
    #do something
by (4,088 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 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.