Search code examples
c#unity-game-engineraytracing

Unity3d shotgun cone shoot


I'm using raycasts as a shotgun. I randomize direction of each ray:

Vector3 direction = new Vector3 (UnityEngine.Random.Range (-splash, splash), UnityEngine.Random.Range (-splash, splash), 100);

Everything works fine, but that maximum "range" of this direction is rectangle and I want an ellipse. It's hard to explain: Raycast It's seems I should use normalization somewhere, but I don't know where.


Solution

  • Coordinates of points on circle can be calculated like this x = r*cos(a), y = r*sin(a). If r is random points will be inside of circle with radius of "splash";

    Vector3 direction = new Vector3( Random.Range(-splash,splash)*Mathf.Cos(Random.Range(0,2*Mathf.PI)),Random.Range(-splash,splash)*Mathf.Sin(Random.Range(0,2*Mathf.PI)),0 );