Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | RohanLockley |
Hello there!
I’m making a rather simple 3d game where i need the player to be aimed at the mouse.
This works for a while, however once i move too far away it stops working.
here is my code in use:
func _physics_process(delta):
ProcessInput(delta) #we get the input of the player
ProcessMovement(delta) #we move the player according to our input
move_and_slide()
func ProcessInput(delta):
#raycast section to look for mouse coordinate in 3d
var space_state = get_world_3d().direct_space_state
#get mouse position
var mouse_position = get_viewport().get_mouse_position()
var params = PhysicsRayQueryParameters3D.new()
params.from = camera.project_ray_origin(mouse_position) #sets ray origin
params.to = camera.project_ray_normal(mouse_position)*10000 # sets ray max distance
params.exclude = []
params.collision_mask = 2 #only seek collision with the right collision mask
var result = space_state.intersect_ray(params)
if result:
lookTarget = result.position
print("Hit at point: ", result.position)
an image of how this code looks in godot
to give some more context: my player scene has a collision box attached that moves with it. I can see this box moving along accordingly so it is not a hierarchy based problem i think.
This is easily visible when i start my player high up in the air. it is responsive at first, but after falling so far the camera cannot render the original location of the box, i cannot rotate my player anymore.
this leads me to think the issue lies with the ‘var space_state = get_world_3d().direct_space_state’ line.
It gets triggered once and never updates afterwards. Neither do i find a way to clear and reset it - i thought it would already run every time the processinput() is called, but clearly i am wrong.