how to make a pellet raycast shotgun

I’m trying to give shotgun dispersal but I don’t know how to edit it to make it work?

public int NumberOfPellets = 5;
        public float ShotRadius = 0.05f;
        public GameObject DecalHoleC;
        public Transform Barrel;
        public GameObject DecalHoleM;

        public GameObject DecalHoleG;

        public GameObject DecalHoleB;
        public GameObject DecalHoleW;

     
        protected override void OnFire(Vector3 dir)
        {


            for (int i = 0; i < NumberOfPellets; i++)
            {


                var xy = Random.insideUnitCircle * ShotRadius;
                var newDirection = dir + transform.TransformDirection(xy);
                FireBullet(newDirection);
                Ray rayorigin = new Ray(Barrel.position, Barrel.forward);
                RaycastHit hitinfo;
                if (Physics.Raycast(rayorigin, out hitinfo))

                {
                    if (hitinfo.collider.tag == "Concrete")
                    {

                        GameObject bulletUse1 = DecalHoleC;

                        Instantiate(DecalHoleC, hitinfo.point, Quaternion.LookRotation(hitinfo.normal));


                        Vector3 direction = hitinfo.point - Barrel.position;
                        Barrel.rotation = Quaternion.LookRotation(direction);


                        Destroy(DecalHoleC, 4);
                    }

Just add a random value to the end of your Ray.

thank you for the answer, but what exactly do you mean, which line or what should I write there ? , thank you :smile:

The one where you create your Ray. The first value is the origin, the second one is the direction the Ray is pointing. If you want to add variance to it, you add it to the direction of the Ray.

Ray rayorigin = new Ray(Barrel.position, Random.Range(Barrel.position)); like this?

Check the documentation on how to use Random.Range. It does not take a Vector3 as input.

I know that, but I don’t know what to put there ?

Ray rayorigin = new Ray(Barrel.position,Random.Range()); <============ ?????????

it works now thanks for your help :smile:Ray rayorigin = new Ray(Barrel.position, Barrel.forward + newDirection);