How to change position to mouse position when clicking-and-dragging?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By GreenTreeStudios-2.5

Good evening, it’s me again. I’m having trouble figuring out why my code is not changing the RayCast3D’s target position to the newly clicked position when the left mouse button is held down and the mouse is dragging:

This is the code I’m using:

    var mouse
   var cam = get_node("Camera3D")
   @onready var cameraRayCast = get_node("RayCast3D")

    mouse = get_viewport().get_mouse_position()
	var from = cam.project_ray_origin(mouse)
	var to = from + cam.project_ray_normal(mouse) * 100
	cameraRayCast.target_position = to

if anyone can help me figure out what I’m doing wrong or what I’m missing, it would be much appreciated. Thank you in advance.

:bust_in_silhouette: Reply From: Enfyna

Put your last 4 lines of code a input(event) function :

func _input(event):
    if event is InputEventMouseButton:
        mouse = get_viewport().get_mouse_position()
        var from = cam.project_ray_origin(mouse)
        var to = from + cam.project_ray_normal(mouse) * 100
        cameraRayCast.target_position = to

this will work everytime a mouse click is received. If you want to add dragging you probably have to check if the event is InputEventMouseMotion and if the mouse is pressed too.