//The void below will create random rotations an x number of times depending on the value form the fragments variable //In addition the void will apply damage and force to object that have a baddieController component and/or a rigidbody void BulletDirection() { for (int i = 0; i < fragments; i++) { Quaternion fireRotation = Quaternion.LookRotation(gunEnd.transform.forward); Quaternion randomRotation = Random.rotation; RaycastHit hit; fireRotation = Quaternion.RotateTowards(fireRotation, randomRotation, Random.Range(0.0f, spreadAngle)); if (Physics.Raycast(gunEnd.transform.position, fireRotation * Vector3.forward, out hit)) { baddieController enemy = hit.transform.GetComponent <baddieController>(); Rigidbody RB = hit.transform.GetComponent <Rigidbody>(); Debug.DrawLine(gunEnd.transform.position, hit.point, Color.red, 1f); if (enemy != null) { enemy.Health(Damage); } if (RB != null) { RB.AddForce(-hit.normal * force); } } } }
// Update is called once per frame void Update() { //This will center the raycast to the fire from the middle of the screen Ray ray = cam.ScreenPointToRay(new Vector2(cam.scaledPixelWidth / 2, cam.scaledPixelHeight / 2)); RaycastHit hit; //The line of code below will restet the rifle to it original position transform.localPosition = Vector3.Lerp(transform.localPosition, Vector3.zero, recoilDamping * Time.deltaTime); //The code below will allow the rifle to fire multiple times, play the guns audio and add recoil if (Input.GetMouseButton(0) && Time.time >= nextFire && AmmoPacks != 0 && PlayerControlls.isDead == false) { nextFire = Time.time + 1f / fireRate; flash.Play(); Ammo--; shoot.Play(); transform.position -= transform.parent.forward * recoil; //The code below here will manage how much ammo and ammo packs you have if (Ammo <= 0 && AmmoPacks != 0) { Ammo = 30; reload.Play(); AmmoPacks--; } if (AmmoPacks == 0) { Ammo = 0; AmmoPacks = 0; } if (Ammo == 0) { Ammo = 0; } //When the raycast hits an object it will look for a baddieController componet in the object and apply damage if it finds one if (Physics.Raycast(ray, out hit)) { baddieController enemy = hit.transform.GetComponent <baddieController>(); if (enemy != null) { enemy.Health(Damage); } } } }