need some help with script

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By swipis
:warning: Old Version Published before Godot 3 was released.

hi guys!
Need some help with script. So I trying to shoot ray when I press mouse button, here is the code:

func _input(event):
if event.type == InputEvent.MOUSE_BUTTON and event.pressed and event.button_index == 1:
	ray_from = self.project_ray_origin(event.pos)
	ray_to = ray_from + self.project_ray_normal(event.pos) * ray_length
	
	ray.set_cast_to(ray_to)
	
	print(ray_to, " cast_to ", ray.get_cast_to())
	
	if ray.is_colliding():
		var col1 = ray.get_collider()
		print("colliding with ", col1.get_name())
		#if col1.get_name() == "hitbox":
		#	print("collision detected with: ", col1.get_name())
		#else:
		#	print("no collision detected!")
		ray.set_cast_to(Vector3(0,0,0))
		
		print(ray.get_cast_to())

Problem is when I press mouse button once code setting up set_cast_to but not checking collision test, when I press mouse button second time on same coordinates I getting collision test. I thought that on mouse button click will be executed all code not just part of the code,why is that?
enter image description here
thank you

:bust_in_silhouette: Reply From: eska

The collisions of the RayCast and RayCast2D nodes are only updated once per physics frame (fixed_process). When changing cast_to, you have to wait for the next _fixed_process callback before checking get_collider().

If you must check immediately, you can use the more complex functions PhysicsDirectSpaceState.intersect_ray for 3D and Physics2DDirectSpaceState.intersect_ray for 2D.

thank you, finally I got an answer :slight_smile:

swipis | 2016-03-23 19:13

Note that DirectSpaceState is available only in _fixed_process

Bojidar Marinov | 2016-03-23 19:56