Random projectile accuracy?

Hello all.

I’m trying to make a project where when an arrow is spawned, the emitter’s accuracy is ever so slightly at a random angle to give it a random precision.

I remember some python module called Mathules or something that worked for this, but I can’t even look up the documentation for it.

Any of you have a low recourse method for doing this? I’d really appreciate it.

Thank you for your time.

from random import uniform

spray = 0.001

rot = arrow_spawner.worldOrientation.copy().to_euler()
rot.x += uniform(spray, -spray)
rot.y = 0
rot.z += uniform(spray, -spray)
        
arrow_spawner.worldOrientation = rot

Should do the trick, you get something like this:

Honestly, you’re the demigod of Blender game engine. Thank you again so much. Worked perfectly.

1 Like

I used to do this by having the empty that spawned the bullets playing an animation of it moving in a (somewhat) random pattern.

Also it’s worth noting that Catak’s solution will exhibit Random Walk and Brownian Motion. Given long enough it could shoot 90 degrees from where the player is aiming.
Here’s a tweak that slowly “centers” the crosshairs to the original position, which should fix it:

damping = 0.01

rot = arrow_spawner.localOrientation.copy().to_euler()

# Ensure that the rotation tends towards [0,0,0]
# to prevent the aim wandering too far.
rot.x *= (1.0 - damping)
rot.z *= (1.0 - damping)

rot.x += uniform(spray, -spray)
rot.y = 0
rot.z += uniform(spray, -spray)

arrow_spawner.localOrientation = rot

Note that it now uses local orientation and assumes it should have zero rotation for forwards, so you may need to parent it to another empty facing the way you want the shots to go.

2 Likes

Oops yes indeed, forgot to say after shooting to put localOrientation = [0,0,0]. (sorry was middle in the night, bit tired) But your solotion is even cleaner.

screw rotations, vectors are the way.

acc = 0.1
rndx = (logic.getRandomFloat()-0.5)*acc
rndy = (logic.getRandomFloat()-0.5)*acc
rvec = owner.getAxisVect((rndx,5,rndy))

ammo = owner.scene.addObject("AmmoObj", owner, 0)
ammo.alignAxisToVect(rvec, 1, 1.0)
1 Like

Here something I use since I don’t code much, I just animate the emitter of the projectile to move ever so slightly all the time, so whenever you shoot your projectile is shot at a different angle.