raycasting from camera and limited areas ? /

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By morningkingdom

Hi, im raycasting from camera and using return value for teleportation skill.

And its work nicely, but there are areas that i dont want to player can teleport. Like offlimit of the map. There are level limits (staticbody) and thats okay player cant go beyond that, but he/she can teleport beyond there :D, so im trying to figure out some way to prevent this.

i did find two way:
first add few areas around the map and add them to a group named “cant_teleport_area”,
after that, did add few line of code in my teleport script:

       var in_can_teleport_area = false
    
    
    func _physics_process(delta):
## this part get teleportation info from touching ##
        			var ray_from = camera.project_ray_origin(teleport_touch_position )
        			var ray_to = ray_from + camera.project_ray_normal(teleport_touch_position) * TELEPORT_RAY_LENGTH 
        			
        			var space_state = get_world().direct_space_state 
        			var result = space_state.intersect_ray(ray_from, ray_to)
## this part get teleportation info from touching ##

##this part checking if raycast is colliding with cant_teleport_areas ##
        			var raycast_node = get_parent().get_node("Camera_for_tele_raycast/camera_raycast")
        			raycast_node.translation = ray_from
        			raycast_node.cast_to = ray_to
        			if raycast_node.is_colliding():
        				print ("colliding")
        				var get_collider = raycast_node.get_collider()
        				if not get_collider.is_in_group("cant_teleport_area"):
        					print ("is in area")
        					in_can_teleport_area = true
        				else:
        					in_can_teleport_area = false
##this part checking if raycast is colliding with cant_teleport_areas ##



        			if (result.empty() == false and in_can_teleport_area == true):
                                   ## do teleport stuff
        				in_can_teleport_area = false

and it did not work, i work on it a lot like, i did it a reverse way, add a area in the map only teleportation can be done etc. i did not get any results.

second way is;
insteed of code i did use collision mask layers.
i placed the area nodes that i created for first methot (cant_teleport_areas) to a physic layer named cant_teleported_layer.

After that check the raycast collision mask and uncheck the cant_teleported_layer, so that raycast will not work on that areas. (of cource i did also make sure that areas that cant be teleported belong the cant_teleported_layer)

and it did kind of work this time but not nice and smoot.
you still can get passby the areas and can teleport inside of them. i did try diffrent shapes for collision shapes but did not see any difference.
Finally here i am, looking for an anwser.

bdw, game is topdown shooter / 3d

1 Like
:bust_in_silhouette: Reply From: rossunger

If it were me, I would have an area for canTeleport, put it on a separate collision layer and then use raycast to check collision with that area when you try to teleport.

If this is what you tried and it didn’t work, please let us know what wasnt working and how you debugged it!

  • was the collision not happening?
  • was something else colliding when it shouldn’t be?
  • did you check your collision layers and masks? Did you check your signals? Did you check your raycast settings?

thanks for the anwser,
i did what you suggest and result is the same with the my second way. its kind of work but sometimes it does not, there is a problem but i dont know what it is.

but i susspect with raycastig,
there are death zones (area nodes), i did uncheck those,

and also map lim inits( static body), did the same.

and this is the teleport_area layer settings:

this one raycast:
enter image description here

for debuging, i edit the script a little, i did want to check if there is a collision and if there is, if is in “can_teleport” group?

So after i bind the raycasting from camera to raycast node (child of the camera for raycasting), did test it, but i got noting return as a value. this is script.

			var camera = camera_node
		
			var ray_from = camera.project_ray_origin(teleport_touch_position )
			var ray_to = ray_from + camera.project_ray_normal(teleport_touch_position) * TELEPORT_RAY_LENGTH 
			
			var space_state = get_world().direct_space_state 
			var result = space_state.intersect_ray(ray_from, ray_to)
			if result.empty() == false:
do teleporting
##  this part check the return values
var raycast_node = get_parent().get_node("Camera_for_tele_raycast/camera_raycast")
				raycast_node.translation = ray_from
				raycast_node.cast_to = ray_to
				if raycast_node.is_colliding():
					var get_collider = raycast_node.get_collider()
					print ("colliding")
					print (get_collider.name)
					if get_collider.is_in_group("teleport_area"):
						print ("is in area")

like there is no collision, noting.

i think because of i disable the “input_ray_pickable” from the nodes that, i want the raycast stay away, its kind of work. but “can_teleport_area” has no reaction, where could be the problem ?

morningkingdom | 2022-02-16 18:51

If you never managed to solve this:-

You can add your floor tiles to a group and only check for intersections with this group.
Groups are different from layers and sort of hidden in the inspector. I started using them recently as I could not get exceptions to work in the native intersect_ray() function for some reason. (although now thinking about it I was trying to check for collisions with meshes rather than static bodies)

Groups work amazing, I think there’s no limit to the amount of groups you can have but I could be sorely mistaken.

LordBoots | 2022-11-25 09:50