I am trying to build something in 3d environment which allows player to grab objects and swipe them across a table i have managed to make node responsive and move upon click yet not as i desired.I have used an input event on a collision shape to detect grabbing and moved on to parent node to cast a ray and determine the cursor position to move it on that object.
this is my code for the object which is being dragged:
extends Spatial
var beingdragged=0
signal dragme
const ray_length = 10
func _ready():
pass
func _on_RigidBody_input_event(camera, event, click_position, click_normal, shape_idx):
if (event is InputEventMouseButton):
beingdragged=1
emit_signal("dragme")
var clickZone=get_node("./RigidBody")
clickZone.disconnect("input_event",self,"_on_RigidBody_input_event")
and here is the code where i cast ray to determine cursor location and do certain translations.
extends Spatial
var laser_color = Color(.867, .91, .247, 0.1)
const ray_length = 200
var dragGoingON
var intersection
func _ready():
pass
func _on_Clam_dragme():
print ("now we can handle ray casting operations")
dragGoingON=1
func _on_StaticBody_input_event(camera, event, click_position, click_normal, shape_idx):
if (event is InputEventMouseButton):
print(event)
dragGoingON=0
var clickZone=get_node("Clam/RigidBody")
clickZone.connect("input_event",get_node("Clam"),"_on_RigidBody_input_event")
if(dragGoingON == 1):
intersection=0
var from = camera.project_ray_origin(event.position)
var to = from + camera.project_ray_normal(event.position) * ray_length
var space_state = get_world().direct_space_state
var result = space_state.intersect_ray(from, to)
intersection=result.position
var clamUnderControl=get_node("Clam")
clamUnderControl.translation=intersection
When i use this snippet object locks to cursor but allways stays far in a far location random at each play state.
Thank you for reading.