Topic was automatically imported from the old Question2Answer platform.
Asked By
Curly Brace
Old Version
Published before Godot 3 was released.
I want to use gui in 3d space, so i’ve made a viewport with my gui and it is rendered into quad, that has an area node, that forwards mouse events into viewport. all work fine.
but i want to use this in first person view, so i’ve made a simple controller that rotates camera around with LOCKED mouse. so that gui does not work anymore.
how to make gui react to the center of the screen? i think it is concerned raycasting from camera on every frame and area should somehow create and forward events, but i don’t know how to make this exactly (is this even a solution?)
actually the thing is to have a RayCast node as a child of Camera node, put a script here, that will wait for input and then check whether it is a mouse input.
if it is mouse input, it will check if ray (this node actually) is colliding something (also a good idea to put that ray and all 3d gui stuff on separate layer). if it is, it will take a colliding object, the collision point and calc local coordinates by taking transform.affine_inverse() * collision_point.
after that there will be coordinates local to the area node of the gui thingy. i personaly had a viewport 150x150, so i’ve first added Vector(0.5, 0.5) to the point, and then multiplied this by Vector(150, 150).
my viewport was flipped verticaly, so i needed to invert y coordinate.
after that i took the actuall event (parameter of _input) and changed pos and global_pos to my new point. it is also a good idea to make use of mouse motion by storing previous position here in script and make relative_pos = cur_pos - prev_pos.