I'm attempting to scatter the bullets of a shotgun from the center of the screen:
var vp_size = get_viewport().size
var from = cam.project_ray_origin(vp_size * 0.5)
var to = cam.project_position(vp_size * 0.5 + extra, distance)
var space_state = get_world().direct_space_state
var result = space_state.intersect_ray(from, to, [ player], 2147483647, true, true )
I'm passing extra
as a random Vector2, the x
and y
being a range from negative to positive:
for i in 6:
var x = rand_range(-5000.0, 5000.0)
var y = rand_range(-5000.0, 5000.0)
bullet_fire(Vector2(x,y))
The trouble is, it has to be a weirdly large size to do anything, in this case between -5000 and +5000. The other problem is, it seems to change depending on the window size, so I instead tried:
for i in 6:
var vp_size = get_viewport().size
var x = rand_range(-vp_size.x * 5, vp_size.x * 5)
var y = rand_range(-vp_size.y * 5, vp_size.y * 5)
bullet_fire(Vector2(x,y))
Which works more consistently, but I'm sure there must be a better way of doing this. I find project_position()
easier to use than project_ray_normal()
, but that might give me more consistent results?