I’m trying to make a Raycast2D cast to a point that the player clicks on with a mouse (the Raycast2d is tied to the player node). The issue I’m having is “$RayCast2D.target_position = (get_global_mouse_position() - get_parent().global_position)” is causing the point to appear opposite from where i clicked(not exactly but relative). How should i go about it so the Raycast2D is properly cast to the point the player clicks on?
func _input(event):
if event.is_action(“Click”):
var mouse_pos = get_global_mouse_position()
$RayCast2D.target_position = (get_global_mouse_position() - get_parent().global_position)
$RayCast2D.force_raycast_update()
if mouse_pos.distance_to(get_parent().global_position) < radius_range and is_ready and !$RayCast2D.is_colliding():
is_ready = false
$CooldownTimer.start()
get_parent().global_position = get_global_mouse_position()
To make the RayCast2D cast to the correct point where the player clicks, you should adjust the target position relative to the RayCast2D’s parent node. Here’s a simplified version of your function:
func _input(event):
if event.is_action(“Click”):
var mouse_pos = get_global_mouse_position()
$RayCast2D.target_position = mouse_pos - get_parent().global_position
Ensure that the RayCast2D node is a child of the player node and that the script is attached to the player node as well. This should make the ray cast to the correct point.
When I tried your code my issue still persisted. Here’s a couple pictures with the red line being the vector and the red circle being approx where i clicked my mouse.
Fixed the issue. I had the script tied to a child 2d Node attached to the player. When I moved the code around to work in the player script the inverting issue stopped happening.
No idea why it resolved the issue though as it had the same global positioning as the Player Node. If anyone knows why feel free to send a message as i am curious.
Thank you both for your assistance and have a wonderful day!