Mouse position and raycasting !

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

Hello here my code !

func _input(event: InputEvent) -> void:
#if event is InputEventMouseButton and event.pressed and event.button_index == 1:
if Input.is_action_just_pressed("magic"):
	
	var from = camera_3d.project_ray_origin(event.position)
	var to = from + camera_3d.project_ray_normal(event.position) * RAY_LENGTH
	print("from" , from)
	print("to", to)

My question ! i want to have the raycast from my mouse while pressing another button than the mouse. note that the event input is on comment just to show .

Edit: Yo ! I would like to have a “Cross Aim” on the end of the ray cast to see where i raycast because my mouse is hidden because it control the camera !

im trying this too but i cant get the position of collision … ! And i dont understand the code i took it on the godot docs … And im not sure of how it work.

if Input.is_action_just_pressed("magic"):
	var direct_state = get_world_3d().direct_space_state
	var query = PhysicsRayQueryParameters3D.create(transform.origin, Vector3(-100,0, 0))
	
	var collision1 = direct_state.intersect_ray(query)
	print(self.position)
	print(collision1)
:bust_in_silhouette: Reply From: purple_mage

HI here my new code !

if Input.is_action_just_pressed("magic"):
	
	var mousePos = get_viewport().get_mouse_position()
	var from = camera_3d.project_ray_origin(mousePos)
	var to = from + camera_3d.project_ray_normal(mousePos) * RAY_LENGTH
	var space = get_world_3d().direct_space_state
	var rayQuery = PhysicsRayQueryParameters3D.new()
	rayQuery.from = from
	rayQuery.to = to
	var result = space.intersect_ray(rayQuery)
	print(result.position)
	aim.position = result.position

Now the problem is i raycast my hero ! XD . How can i ignore him !

:bust_in_silhouette: Reply From: purple_mage

Ok it work now ! here my code . Now my problem is that it crash when i look in the void ! because my level isnt big

func test2()-> void:	
var mousePos = get_viewport().get_mouse_position()
var from = Vector3(self.position.x , 10 ,self.position.z)
var to = from + camera_3d.project_ray_normal(mousePos) * RAY_LENGTH
var space = get_world_3d().direct_space_state
var rayQuery = PhysicsRayQueryParameters3D.new()
rayQuery.from = from
rayQuery.to = to
var result = space.intersect_ray(rayQuery)
print(result.position)
aim.position = result.position
:bust_in_silhouette: Reply From: purple_mage

Final code ! thanks all !

func aiming()-> bool:	
var mousePos = get_viewport().get_mouse_position()
var from = Vector3(self.position.x , 8 ,self.position.z)
var to = from + camera_3d.project_ray_normal(mousePos) * RAY_LENGTH
var space = get_world_3d().direct_space_state
var rayQuery = PhysicsRayQueryParameters3D.new()
rayQuery.from = from
rayQuery.to = to
var result = space.intersect_ray(rayQuery)
if result.size()>1:
	print(result.position)
	aim.position = result.position
	return true
else:
	return false