I have an enemy with RayCast2D attached acting as their vision. At the moment, it stays pointing down when they move, and I am wondering if I can rotate it to the direction that the enemy velocity is going so that the vision is accurate
You can convert a Vector2 into an angle using the angle() function and then apply it to the rotation of your Raycast2D. You will notice your angle to be offset by 90° though if your Raycast2D points downwards. To fix that, change it’s default target position to be pointing to the right, which Godot treats internally as a 0° angle. Add this code to your enemy’s physics process but AFTER any velocity changes have been applied and it should do the trick. :>
func _physics_process(delta: float) -> void:
# Your other code here
$RayCast2D.rotation = velocity.angle()
Your Raycast2D will snap back to facing right whenever you are at 0 velocity however. If you want to keep it facing the last direction it moved, change the code to this:
func _physics_process(delta: float) -> void:
# Your other code here
if velocity:
$RayCast2D.rotation = velocity.angle()