Shotgun raycast problems

Hey!

I created this script with help of some other questions and the answers to them, so if you recognize any code, it’s probably from there.
I’m trying to create a shotgun with raycasting, I’ve got everything working except that it’s shooting downwards and that it’s originating from the actual shotgun, not the camera like I want.

Here’s my script:

    public int DamageAmount = 5;
    public float TargetDistance;
    public float AllowedRange = 100;
    public Transform Effect;

    public float scaleLimit = 2.0f;
    public float z = 10f;

    public int count = 8;


    // Use this for initialization
    void Start () {
       
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetButtonDown("Fire1"))
        for (int i = 0; i < count; i++)
        {
            ShootRay();
        }
    }

    void ShootRay()
    {
        float radius = Random.Range(0, scaleLimit);
        float angle = Random.Range(0, 10 * Mathf.PI);

        Vector3 direction = new Vector3(radius * Mathf.Cos(angle), (radius * Mathf.Sin(angle)), z);
        direction = transform.TransformDirection(direction);

        RaycastHit hit;
        Ray r = new Ray(transform.position, direction);
       

        if (Physics.Raycast(r, out hit))
        {
            Debug.Log(hit.collider.gameObject);
            TargetDistance = hit.distance;
            if (TargetDistance < AllowedRange)
            {
                var particleClone = Instantiate(Effect, hit.point, Quaternion.LookRotation(hit.normal));
                Destroy(particleClone.gameObject, 2);
                hit.transform.SendMessage("DeductPoints", DamageAmount, SendMessageOptions.DontRequireReceiver);
                Debug.DrawLine(transform.position, hit.point);
            }
        }
    }

To use the camera’s direction as a ray, there’s a function for that:

// from the center of the viewport
Ray ray = Camera.main.ViewportPointToRay(Vector2.one * 0.5f);

You could also use the Camera.main.transform.forward for just the facing direction.

Thank you for trying to answer my question. I did know that you could do that, the only problem is that this doesn’t work together with the rest of the script. It just shoots all rays to the same point (obviously). Any way I can incorporate the “direction” into this?

Your code is only firing one ray.

    RaycastHit hit;
    List<Ray> rays = new List<Ray>();

    for (int i = 0; i < numberOfRaysToShoot; i++)
    {
        rays.Add(Camera.main.ViewportPointToRay(new Vector2(Random.value, Random.value));
    }

    foreach (Ray ray in rays)
    {
        if (Physics.Raycast(ray, out hit))
        {
            // Do the thing
        }
    }

This will spray rays all over the screen, so you might want to tweak the ViewportPointToRay argument.

I love binomial distributions for this sort of thing, it clusters results around the centre but still allows outliers.

private float binomial()
{
    return (Random.value + Random.value) / 2f;
}

private Vector2 binomialVector2()
{
    return (new Vector2(binomial(), binomial());
}
3 Likes

Thanks! This worked perfectly and exactly like I wanted.

For anyone wondering in the future I used it like this:
Ray r = Camera.main.ViewportPointToRay(new Vector2(Random.Range(0.4f, 0.6f), Random.Range(0.4f, 0.6f)));

2 Likes