I’m working on a FPS, I’m trying to get a random bullet spread working (I’m using a ray for the bullet) I tried a random rotation, however I can’t get it working correctly. I’m looking for a cone type bullet spread.
hi welcome aboard,
if you don’t bother with distances but assume that anything within an imaginary circle around your mouse cursor get a % chance to get shot , you can use the camera ray
But first,
→ get mouse position : http://bgepython.tutorialsforblender3d.com/Mouse/position
MM = [X float, Y float] on [0 → 1]
After, just take a random AA float [0,pi*2] for the angle and random DD float [0,circle diameter] to get a random CC [X float, Y float] point inside that circle :
CC = [cos(AA) * DD , sin(AA) * DD]
(you want DD to be a little fraction of the [0,1] screen , so take something like 0.05 so the circle will be 5% width and 5% height of your current screen)
Now, just have to relocate the CC around MM : TT = [Xmm - Xcc , Ymm - Ycc ]
Cool, now just have to know if that random TT point on the screen (which is within that circle ofc) is hitting an object on screen … so here, you gonna use the camera
→ http://bgepython.tutorialsforblender3d.com/Camera/getScreenRay
well, not tested, but should work
import bge
from math import cos, sin
xM , yM = bge.logic.mouse.position
aa = bge.logic.getRandomFloat() * 3.1415 * 2
dd = bge.logic.getRandomFloat() / 20
xT , yT = xM - cos(aa) * dd , yM - sin(aa) * dd
# get 1st object with property "Blue_Player" being hit by a ray that goes
# from the camera in the direction of our TT random point
obj = cam.getScreenRay(xT, yT, 0.0, "Blue_Player")