![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | pnptr |
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.
Hey um, could you reformat your question better? It’s a little hard to read.
To format multi-lined code, you must skip a line after the last one, then start with four spaces.
One space
Two spaces
Three spaces
Four.
You must make sure the subsequent lines of code have
at least four spaces as well.
Mono fonts
are best used with a single line of code or better yet, just a property, function or variable name.
SIsilicon | 2018-11-17 15:15
Thanks for the format tips i will keep them in mind next time i post a question or answer.While waiting for approval i have resolved the issue and i don’t need to ask this question anymore thank you in advance.
pnptr | 2018-11-17 22:08
updated original post for better looking
volzhs | 2018-11-17 23:45