// Below Coroutine used to simulate burst fire IEnumerator BurstFire() { for (int i = 0; i < 3; i++) { nextFireBurst = Time.time + fireRateBurst; StartCoroutine(ShotEffect()); // Call our Shot Effect Coroutine ammoCount--; // Iterate ammoCount by one down each shot // Spawn Point for ray, always centered to center of camera, this takes a position relative to the camera and converts it to a world point Vector3 rayOrigin = fpsCam.ViewportToWorldPoint(new Vector3(0.5F, 0.5F, 0.0F)); RaycastHit hit; // Holds the information returned from our array if it hits a gameObject with a collider attached // Determine start and end positions for our raycast line when the player fires, first we need to specficy two points for lineRenderer to draw between laserLine.SetPosition(0, bulletSpawn.position); // Our Start Point for our raycast to be shot out of the gun if (Physics.Raycast(rayOrigin, fpsCam.transform.forward, out hit, weaponRange)) { laserLine.SetPosition(1, hit.point); // index position 1 of Hit array, set to the world space position of object colliding with array. TargetPoints targetHealth = hit.collider.GetComponent <TargetPoints>(); // Using hit, store scripting reference of TargetPoints in targetHealth if (targetHealth != null) // If the target hit did have TargetPoints attached then... { // Call healthCalculator function of TargetPoints and pass in this gun's Damage as a parameter targetHealth.healthCalculator(gunDamage); } if (hit.rigidbody != null) // If the target hit did have a rigidbody attached then... { // AddForce to target in the direction negative to its normals and with hitForce as a multiplier hit.rigidbody.AddForce(-hit.normal * hitForce); } } else { laserLine.SetPosition(1, rayOrigin + (fpsCam.transform.forward * weaponRange)); // index position 1 of Hit array, set to the world space position of object colliding with array } Instantiate(casing, casingSpawn.position, casingSpawn.rotation); // -- Test code for ejecting casings -- This is the only one that works because of Game Object Reference not working Debug.Log("Burst Shot Fired"); yield return(new WaitForSeconds(0.1F)); } }
// Update is called once per frame void Update() { ammoCountText.text = ammoCount.ToString() + " / " + "Infinite"; // Display text of ammoCount // Semi-Automatic if (Input.GetButtonDown("Fire1") && Time.time > nextFireSingle && indexFireMode == 1 && hasAmmo == true) { nextFireSingle = Time.time + fireRateSingle; // Incriment nextFire so that the player must wait to fire again StartCoroutine(ShotEffect()); // Call our Shot Effect Coroutine // Spawn Point for ray, always centered to center of camera, this takes a position relative to the camera and converts it to a world point Vector3 rayOrigin = fpsCam.ViewportToWorldPoint(new Vector3(0.5F, 0.5F, 0.0F)); RaycastHit hit; // Holds the information returned from our array if it hits a gameObject with a collider attached // Determine start and end positions for our raycast line when the player fires, first we need to specficy two points for lineRenderer to draw between laserLine.SetPosition(0, bulletSpawn.position); // Our Start Point for our raycast to be shot out of the gun if (Physics.Raycast(rayOrigin, fpsCam.transform.forward, out hit, weaponRange)) { laserLine.SetPosition(1, hit.point); // index position 1 of Hit array, set to the world space position of object colliding with array. TargetPoints targetHealth = hit.collider.GetComponent <TargetPoints>(); // Using hit, store scripting reference of TargetPoints in targetHealth if (targetHealth != null) // If the target hit did have TargetPoints attached then... { // Call healthCalculator function of TargetPoints and pass in this gun's Damage as a parameter targetHealth.healthCalculator(gunDamage); } if (hit.rigidbody != null) // If the target hit did have a rigidbody attached then... { // AddForce to target in the direction negative to its normals and with hitForce as a multiplier hit.rigidbody.AddForce(-hit.normal * hitForce); } } else { laserLine.SetPosition(1, rayOrigin + (fpsCam.transform.forward * weaponRange)); // index position 1 of Hit array, set to the world space position of object colliding with array } ammoCount--; // Iterate ammoCount by one down each shot Instantiate(casing, casingSpawn.position, casingSpawn.rotation); // -- Test code for ejecting casings -- This is the only one that works because of Game Object Reference not working Debug.Log("Single Shot Fired"); } // Burst if (Input.GetButtonDown("Fire1") && Time.time > nextFireBurst && indexFireMode == 2 && hasAmmo == true) { StartCoroutine(BurstFire()); } // Automatic if (Input.GetButton("Fire1") && Time.time > nextFireAutomatic && indexFireMode == 3 && hasAmmo == true) { nextFireAutomatic = Time.time + fireRateAutomatic; StartCoroutine(ShotEffect()); // Call our Shot Effect Coroutine // Spawn Point for ray, always centered to center of camera, this takes a position relative to the camera and converts it to a world point Vector3 rayOrigin = fpsCam.ViewportToWorldPoint(new Vector3(0.5F, 0.5F, 0.0F)); RaycastHit hit; // Holds the information returned from our array if it hits a gameObject with a collider attached // Determine start and end positions for our raycast line when the player fires, first we need to specficy two points for lineRenderer to draw between laserLine.SetPosition(0, bulletSpawn.position); // Our Start Point for our raycast to be shot out of the gun if (Physics.Raycast(rayOrigin, fpsCam.transform.forward, out hit, weaponRange)) { laserLine.SetPosition(1, hit.point); // index position 1 of Hit array, set to the world space position of object colliding with array. TargetPoints targetHealth = hit.collider.GetComponent <TargetPoints>(); // Using hit, store scripting reference of TargetPoints in targetHealth if (targetHealth != null) // If the target hit did have TargetPoints attached then... { // Call healthCalculator function of TargetPoints and pass in this gun's Damage as a parameter targetHealth.healthCalculator(gunDamage); } if (hit.rigidbody != null) // If the target hit did have a rigidbody attached then... { // AddForce to target in the direction negative to its normals and with hitForce as a multiplier hit.rigidbody.AddForce(-hit.normal * hitForce); } } else { laserLine.SetPosition(1, rayOrigin + (fpsCam.transform.forward * weaponRange)); // index position 1 of Hit array, set to the world space position of object colliding with array } ammoCount--; // Iterate ammoCount by one down each shot Instantiate(casing, casingSpawn.position, casingSpawn.rotation); // -- Test code for ejecting casings -- This is the only one that works because of Game Object Reference not working Debug.Log("Automatic Shot Fired"); } // Fire Modes if (indexFireMode == 0 && consoleTextDisplayed == false) { // Console Alert: FireMode set to safe Debug.Log("Safe"); // Play FireMode Switch Sound Effect //aud.PlayOneShot(FireModeSoundEffect); consoleTextDisplayed = true; // FireMode has been changed so, Console has displayed text } if (indexFireMode == 1 && consoleTextDisplayed == false) { // Console Alert: FireMode set to single Debug.Log("Single"); // Play FireMode Switch Sound Effect //aud.PlayOneShot(FireModeSoundEffect); consoleTextDisplayed = true; // FireMode has been changed so, Console has displayed text } if (indexFireMode == 2 && consoleTextDisplayed == false) { // Console Alert: FireMode set to burst Debug.Log("Burst"); // Play FireMode Switch Sound Effect //aud.PlayOneShot(FireModeSoundEffect); consoleTextDisplayed = true; // FireMode has been changed so, Console has displayed text } if (indexFireMode == 3 && consoleTextDisplayed == false) { // Console Alert: FireMode set to automatic Debug.Log("Automatic"); // Play FireMode Switch Sound Effect //aud.PlayOneShot(FireModeSoundEffect); consoleTextDisplayed = true; // FireMode has been changed so, Console has displayed text } if (indexFireMode > 3 && consoleTextDisplayed == false) { // Console Alert: FireMode Reset Debug.Log("Switch FireMode Reset"); indexFireMode = 1; // FireMode set back to one consoleTextDisplayed = false; // FireMode has been changed so, Console has displayed text } // Below If Statement checks to see if FireMode is presed if (Input.GetButtonDown("FireMode")) { // Console Alert: Switch Station Button Pressed Debug.Log("Switch FireMode Button Pressed"); //aud.Stop(); // Stop Playing the Previous Track indexFireMode++; // Then Incriment indexStation to change station track played consoleTextDisplayed = false; // Radio station has been changed so, Console has displayed text } // Ammo Management and Reload if (Input.GetButtonDown("Reload")) { aud.PlayOneShot(gunEquipReload, 1.0F); // Play Reload Sound Effect ammoCount = 30; hasAmmo = true; if (ammoCount <= 0) { Instantiate(magEmpty, magEmptySpawn.position, magEmptySpawn.rotation); // Spawn empty mag if ammoCount is 0 } if (ammoCount > 0) { ammoCount = 30 + 1; Instantiate(magFull, magFullSpawn.position, magFullSpawn.rotation); // Spawn full mag if ammoCount is greater than 0 } } if (ammoCount <= 0) { hasAmmo = false; } }