(Resolved)Move a 3D object with the mouse pos

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Trinketos
:warning: Old Version Published before Godot 3 was released.

Hi all.
First, I can not explain myself well in english or spanish
and second, I come from unity engine
Here is the script

 extends Spatial
export (NodePath) var building_path
onready var building = get_node(building_path)
pos = Vector2()
func _input(event):
     if event.type == InputEvent.MOUSE_MOTION:
         pos = Vector2(event.pos.x,event.pos.y)#event.pos to Vector2()
         var new_pos = Vector3(pos.x,1,pos.y)#Convert pos to vector 2
         building.set_translation(new_pos)# set the pos to the building

The problem is that the position of the building is very high something like (960,1,20),
What I try to do is that the building moves in the position of the cursor and follow it, in unity I solved it using raycast, but in godot I do not understand how it works

Good is almost ready, I have problems with height, for example elevated ground (use tiles) but it works.
i like to show ,but i dont know how to upload the script

:bust_in_silhouette: Reply From: Zylann

Take a look at the 3D picking demo project (you can find demos here: Download - Godot Engine)

You can add a Rigidbody/KinematicBody/Area (whatever suits you) to your building, with a CollisionShape, and make it ray pickable:
enter image description here

When you do that, input events will be sent to the collision object (rigidbody/kinematic/area) if it has an _input_event function, as if it was part of the “GUI”, so events happening when the mouse is not over it won’t be received:

func _input_event(camera, event, pos, normal, shape):
    if (event.type==InputEvent.MOUSE_BUTTON and event.pressed):
        ...

This is how you can detect hover and click easily.

Now if you want the building to follow the mouse in 3D… there are multiple ways of doing it, that depends on your game.

FYI if you need to do raycasts, you can do them inside _fixed_process because that’s the time where the physics engine is in sync with scripts.

func _fixed_process(delta):
	# Get the camera (just an example)
	var camera = get_parent().get_node("Camera")
	
	# Project mouse into a 3D ray
	var ray_origin = camera.project_ray_origin(mouse_pos)
	var ray_direction = camera.project_ray_normal(mouse_pos)
	
	# Cast a ray
	var from = ray_origin
	var to = ray_origin + ray_direction * 1000.0
	var space_state = get_world().get_direct_space_state()
	var hit = space_state.intersect_ray(from, to)
	if hit.size() != 0:
		# collider will be the node you hit
		print(hit.collider)

Other manual queries are listed here http://docs.godotengine.org/en/stable/classes/class_physicsdirectspacestate.html

The hit variable is a Dictionary. What it contains is not documented for 3D, but I guess it will be similar to the 2D one: http://docs.godotengine.org/en/stable/classes/class_physics2ddirectspacestate.html#class-physics2ddirectspacestate-intersect-ray

Gracias, No se me habia ocurrido de esa forma, vere si funciona
Thanks, I did not think of it that way, I’ll see if it works

Trinketos | 2017-02-24 19:17

Maybe I’m just too sleep deprived or missing something, but nothing in this answer seems to address the question of how to move the object, as in following the mouse.

woopdeedoo | 2018-08-10 01:53

Indeed, because it depends how you want the object to move. In 3D you can mostly move objects along a surface or a plane, so for example if you have a terrain on which your objects are, you would use raycasts (as described in my answer) to get the hit position on the terrain (eventually using a mask to not pick other objects) and translate the object to that position as you drag the mouse. But it’s just one way amonst others.

Zylann | 2018-08-10 18:45