[3D] Sometimes 'intersect_ray' will not give me a collision

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

Hello,

currently i am trying to do some hitscan weapons. I followed this (https://www.youtube.com/watch?v=4jbfIN4t83k) tutorial but i noticed that sometimes my code would not execute. I tried now for several hours where my mistake might be but so far i have come to no conclusion.

The funny thing is that it always gives me a collision when i aim at the floor but at walls it will sometimes just not execute my code.

You can download my project here https://github.com/fruitdude/raycast-intersect


Player Script

extends Spatial


var mouse_sensitiviy : float = .2

onready var head = $Head
onready var raycast = $Head/Camera/RayCast
onready var muzzle = $Head/WeaponPosition/MeshInstance/Muzzle


func _ready():
	Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)


func _input(event):
	if event is InputEventMouseMotion:
		rotate_y(deg2rad(-event.relative.x * mouse_sensitiviy))
		head.rotate_x(deg2rad(-event.relative.y * mouse_sensitiviy))
		head.rotation.x = clamp(head.rotation.x, deg2rad(-90), deg2rad(90))


func _physics_process(_delta):
	if Input.is_action_just_pressed("fire"):
		if raycast.is_colliding():
			var direct_state = get_world().direct_space_state
			var collision = direct_state.intersect_ray(muzzle.global_transform.origin, raycast.get_collision_point())
			
			if collision:
				print("generated raycast collides with object")
				DrawLine3d.DrawLine(muzzle.global_transform.origin, raycast.get_collision_point(), Color(1, 0, 0), 1)
			else:
				print("error: no collision")

Edit: My questions was answered on reddit and the solution is to extend the intersect_raycast by a little bit. I did this by editing this line

var collision = bullet.intersect_ray(muzzle.global_transform.origin, 
aim_raycast.get_collision_point() + ((aim_raycast.get_collision_point() - 
muzzle.global_transform.origin ).normalized() * 2))

So I can’t guarantee this is a correct response without testing, but what may be happening is that you’re spawning the raycast inside of the object you want to detect as being intersected. I had this problem in a game of mine, when I got too close to an object the raycast that was supposed to detect it would spawn within it, and nothing would get detected.

The fix for that worked for me in that instance was to have a second raycast that was the opposite of the first raycast. So in example:

raycastA spawns at (0,0), raycastA casts to (5,0)
raycastB spawns at (5,0), raycastB casts to (-5,0)

However this wont work if both the spawn and the destination are within the object ofc. If this is indeed the problem, you may want to spawn the raycasts from within the collision of whatever is firing them, to make sure that it can’t possible intersect with the collision of something else.

Like I said though, I haven’t messed with your project, so sorry if this is all irrelevant lol.

denxi | 2021-01-09 02:17