The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates. | Twitter

+1 vote


currently i am trying to do some hitscan weapons. I followed this ( 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

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():

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)
                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))
Godot version 3.2.2.stable
in Engine by (22 points)
edited by

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.

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.