Looking at the code, I think one of the problems is that you are not changing the position of the Raycast
node.
Also, since you are using a Raycast
node instead of using PhysicsDirectSpaceState
, I you will need to change the to
variable a little because Raycast
nodes cast_to
variable points relative to the position of the Raycast
, while in PhysicsDirectSpaceState
you need the positions in global space.
Finally, you need to update the Raycast
and set it out into 3D space with the changed variables, otherwise it will give you results based on the Raycast
node's position, rotate, and scale in the last _physics_process
call.
I think the following code should work (but I have not tested it, so it may not work):
var camera = get_node("Cambase/MainCamera")
var raycast = get_node("Cambase/MainCamera/Raycast")
var from = camera.project_ray_origin(event.position)
var to = camera.project_ray_normal(event.position) * ray_length
""
raycast.global_transform.origin = from
raycast.cast_to = to
raycast.force_raycast_update()
""
Global.MousePos = Vector3(raycast.get_collision_point().x, 0, raycast_get_collision_point().z)
print (str(Global.MousePos))