0 votes

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

in Engine by (51 points)
edited by

1 Answer

+2 votes
Best answer

Take a look at the 3D picking demo project (you can find demos here: https://godotengine.org/download)

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

by (27,766 points)
selected by

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

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.

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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.