- Home /
Generate a random direction within min and max angles
Hello, everyone. I was wondering how I would go about generating a random direction that was within the minimum angle and the maximum angle. I need this functionality in order to simulate bullet spread with my raycasts. Thanks in advance!!
How to you want to define that direction - $$anonymous$$imum/maximum angles in X, Y and Z?
Answer by Berenger · Jun 21, 2012 at 04:07 PM
You should generate two random numbers between min and max (probably -30, +30, something like that. Then use the first number to rotate transform.forward on x and the second number to rotate the result on y.
Answer by tomekkie2 · Jun 21, 2012 at 04:12 PM
Just cast a ray, then get direction Ray.direction with Quaternion.LookRotation(Ray.direction), then add the random spread values to its .eulerAngles.x and .y and apply back to Ray.direction and cast again