Why is this raycast not updating its target position and position?

extends RayCast2D

var player_direction = global_var.player_direction_last
@onready var player = $“…”

func _physics_process(_delta):
position = player.position
target_position = target_position + player_direction * 16
print(position)
print(target_position)
force_raycast_update()
if is_colliding():
print(‘collision’)

The raycast remains in position at the centre of the screen and it’s target position also remains constant. Why is this not updating despite my code trying to do that? player_direction is not the issue and neither is the player node reference as I have been able to confirm these are functioning. The raycast is the child of the player within a player sceen. Help would be much appreciated, thanks in advance!

Because physics requires synchronisation.

Your Node changes are mostly irrelevant between physics steps as they still query the same old physics space state. By changing your RayCast node all you do is changing your start and target position in the query same as you would with a PhysicsRayQueryParameters3D but you still query the same physics space state that has not updated.

That makes sense, thanks! How should I move the raycast instead?

You can move it as you want, but the physics objects themself will only update position after the next physics process step as that is when the physics space state receives an update.

In that case why does the raycast remain in it’s same position? I assume I am using the wrong method to move it given I am only changing it’s starting position as you stated but I am unsure of what method to use to succesfully continously change it to be always infront of the player in a top-down environment. Appologies if I am being unclear I am very new.

The raycast does not stay in position, the physics objects do.

A RayCast2D node is just a node wrapper over a PhysicsRayQueryParameters2D object. When you change the node position and target and call a force update you basically just change the from and to parameters for that next query and do the query again against the current physics space.

You said the positions are not the problem but it looks that is not the case if those positions are not updating. You can always update the parameter positions. Likely there is just something wrong with those position transforms in your node tree but you can test that by using a parameter object as a full replacement of the raycast node. E.g. set the from parameter to the global position and the to parameter to global position + direction to player global position * ray length and fire your ray without a node. If that works it is just the node transform that was the problem.

The node transform being me setting the position and target position of the raycast manually? I’m unsure what is meant by parameter positions and position transforms. In terms of firing a ray without a node, how might this work? is physicsrayqueryparameters2D a function to raycast without a raycast node? Apologies for my confusion and I do intend to learn more about this in the future but I need to fix this issue very quickly. I believe the positions I am trying to set are not an issue but rather the setting of those positions, I am still unclear on how I set the actual position and target position of the raycast so that when it is called upon for collision (to find an intractable object) it may detect those within a space of 16 pixels in-front of the player in whatever direction they may be facing (in my game there are only the four cardinal directions so diagonals can be ignored). Thanks for your help so far, as I have struggled to find even as much explanation as you’ve given. You said I, “still query the same physics space state that has not updated”, how may I query the new physics space state which has been updated or update the prior physics space state so that it reflects the desired values. Thanks in advance.

Sorry for the mess of a reply I tend to write as I think

See raycasting tutorial here Ray-casting — Godot Engine (stable) documentation in English

The physics space is automatically updated by the PhysicsServer after each physics_process() step.

then why does it not update when I change it in a physics process? What do I do to fix the code?