I'm using camera3d.project_ray to intersect with a rotated Area3D so I can place cubes on it with a mouseclick. But the placement is rotating away with the Area3D as shown in the GIF.
code:
var cube = preload("res://cube.tscn")
func _process(delta: float) -> void:
if Input.is_action_pressed("ui_left"):
$Area3D.rotation.y += delta
if Input.is_action_pressed("ui_right"):
$Area3D.rotation.y -= delta
func _input(event: InputEvent) -> void:
if is_left_click(event):
var world_pos = get_world_intersect_pos()
if world_pos == Vector3.ZERO:
return
var new_cube = cube.instantiate()
new_cube.position = world_pos
find_child("cubes").add_child(new_cube)
func get_world_intersect_pos() -> Vector3:
var space_state = get_world_3d().direct_space_state
var cam = $Camera3D
var mousepos = get_viewport().get_mouse_position()
var origin = cam.project_ray_origin(mousepos)
var end = origin + cam.project_ray_normal(mousepos) * 1000
var query = PhysicsRayQueryParameters3D.create(origin, end)
query.collide_with_areas = true
var result = space_state.intersect_ray(query)
if "position" in result:
return result["position"]
else:
return Vector3.ZERO
func is_left_click(event: InputEvent) -> bool:
return event is InputEventMouseButton and event.pressed and event.button_index == MOUSE_BUTTON_LEFT
version: v4.2.1.stable.official [b09f793f5]