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
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.
# 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
Other manual queries are listed here http://docs.godotengine.org/en/stable/classes/class_physicsdirectspacestate.html
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