Skip to content

Instantly share code, notes, and snippets.

@WolfgangSenff
Last active December 24, 2019 00:58
Show Gist options
  • Save WolfgangSenff/8edf8be94f413a663bfb80ac4f8c95f1 to your computer and use it in GitHub Desktop.
Save WolfgangSenff/8edf8be94f413a663bfb80ac4f8c95f1 to your computer and use it in GitHub Desktop.
Touch-move in 3D for Godot 3.0
extends Spatial
signal tapped(card)
signal released(card)
var is_tapped = false setget set_is_tapped
var should_process_touch = true
func set_is_tapped(value):
is_tapped = value
if value:
emit_signal("tapped", self)
else:
emit_signal("released", self)
func _physics_process(delta):
if Input.is_action_pressed("ui_tapped") and should_process_touch:
var mouse_pos = get_viewport().get_mouse_position()
var camera = get_viewport().get_camera()
var ray_from = camera.project_ray_origin(mouse_pos)
var ray_to = ray_from + camera.project_ray_normal(mouse_pos) * 100
var space_state = get_world().direct_space_state
var selection = space_state.intersect_ray(ray_from, ray_to, [], 0x7FFFFFFF, true, true)
if selection and self.name == selection.collider.get_parent().get_name():
self.is_tapped = true
var to_mouse_pos = to_local(selection.position)
translate(Vector3(to_mouse_pos.x, to_mouse_pos.y, 0.0))
if Input.is_action_just_released("ui_tapped") and is_tapped:
self.is_tapped = false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment