Hi, I have added bullet spread on my gun shooting system. But I am having a problem with the bullet spread. Here is the code:-
`var spread = PI * 3
var random = RandomNumberGenerator.new()
var angleX = random.randf_range(-spread, spread)
var angleY = random.randf_range(-spread, spread)

end.x += angleX
end.y += angleY

var query = PhysicsRayQueryParameters3D.create(origin, end)`

If I look at z axis the bullet spreads correctly but if I look at x axis the bullet spreads only on y axis. Please see the image.

  • BirdaoGwra You need to add the offsets along camera current basis axes, not the global coordinate system axes as you're doing now. This will take camera orientation into account:

    end += camera.global_transform.basis.x * angleX
    end += camera.global_transform.basis.y * angleY

    Btw the names angleX and angleY are misleading as in your posted code they work as mere positional offsets, not actual spread angles. But even with angles, using them on separate axes will spread the shots inside rectangular instead of circular area.

Not exactly sure what the left image is supposed to show.

BTW, don't create a new random number generator for each bullet. Either create it beforehand and reuse it or just use the global randf_range() function.

Left image is when I look at z axis, the bullet spreads correctly and the problem happens on right image. When I look at x axis the bullets spreads on y axis only, creates a vertical line.

I still don't understand what... Oh wait. Do you mean you are looking into a different direction in the left image? Let's say you look towards the north in the left image and you look to the west in the right image?

In that case I think your issue is that you simply move your bullt point in global world coordinates. Instead you need to adjust your coordinates relative to the direction you are looking at. So not just move your end point globally up a random amout and globally in the east/west direction a certain amout but relative to your view direction.

Yes when I look at different direction mean if I turn 90 degree left or right. Sorry for the confusion. This is the same problem happening with this unity code also here (sorry if external links are not allowed). Here he just had to add camera.right. But I could not find this in godot.

  • xyz replied to this.

    BirdaoGwra Post the complete code. We don't see the part where you calculate base end point.

    I took the origin and end code from godot raycast doc.

    var mousepos = get_viewport().get_mouse_position()
    var origin = camera.project_ray_origin(mousepos)
    var end = origin + camera.project_ray_normal(mousepos) * ray_length

    • xyz replied to this.

      BirdaoGwra You need to add the offsets along camera current basis axes, not the global coordinate system axes as you're doing now. This will take camera orientation into account:

      end += camera.global_transform.basis.x * angleX
      end += camera.global_transform.basis.y * angleY

      Btw the names angleX and angleY are misleading as in your posted code they work as mere positional offsets, not actual spread angles. But even with angles, using them on separate axes will spread the shots inside rectangular instead of circular area.

      Yeah I know angleX and angleY is misleading. Thanks for your help. This code was giving me headache for last 3 days.