void ShootRay() { ang.x = Mathf.Lerp(ang.x, targetX, upSpeed * Time.deltaTime); transform.localEulerAngles = ang0 - ang; // move the camera or weapon targetX = Mathf.Lerp(targetX, 0, dnSpeed * Time.deltaTime); // returns to rest // Try this one first, before using the second one // The Ray-hits will form a ring //float randomRadius = scaleLimit; // The Ray-hits will be in a circular area float randomRadius = Random.Range(0, scaleLimit); float randomAngle = Random.Range(0, 2 * Mathf.PI); //Calculating the raycast direction Vector3 direction = new Vector3( randomRadius * Mathf.Cos(randomAngle), randomRadius * Mathf.Sin(randomAngle), z ); //Make the direction match the transform //It is like converting the Vector3.forward to transform.forward direction = camera.transform.TransformDirection(direction.normalized); //Raycast and debug Ray r = new Ray(camera.transform.position, direction); RaycastHit hit; if (Physics.Raycast(r, out hit, rangeofShotty)) { Debug.DrawLine(transform.position, hit.point, Color.red); if (Input.GetMouseButtonDown(0)) { SendMessageUpwards("Recoil"); if (hit.collider.gameObject.tag == "enemy") { hbScript.heathManager(15); if (hbScript.health <= 0f) { Destroy(hit.transform.gameObject); } } } } }
void Update() { ang.x = Mathf.Lerp(ang.x, targetX, upSpeed * Time.deltaTime); transform.localEulerAngles = ang0 - ang; // move the camera or weapon targetX = Mathf.Lerp(targetX, 0, dnSpeed * Time.deltaTime); // returns to rest Debug.DrawRay(transform.position, transform.forward * 100f, Color.red); if (gunSwitch == 0) { ArRifle.SetActive(true); RaycastHit hit; if (Input.GetMouseButton(0) && (Time.time > nextPlayTime)) { //GunSound.Play(); nextPlayTime = Time.time + playRate; print(nextPlayTime); GunSound.PlayOneShot(AR); if (GunSound) { i++; } muzzle.Play(); SendMessageUpwards("Recoil"); if (Physics.Raycast(camera.transform.position, camera.transform.forward, out hit)) { print("AR active"); if (hit.collider.gameObject.tag == "enemy") { if (hit.collider.GetType() == typeof(BoxCollider)) { hbScript.heathManager(30); if (hbScript.health <= 0f) { Destroy(hit.transform.gameObject); } } if (hit.collider.GetType() == typeof(SphereCollider)) { hbScript.heathManager(80); if (hbScript.health <= 0f) { Destroy(hit.transform.gameObject); } } } } } } else if (gunSwitch == 1) { ArRifle.SetActive(false); Sniper.SetActive(true); if (snipershot == true) { RaycastHit hit; if (Input.GetMouseButtonDown(0)) { GunSound.PlayOneShot(AR); snipershot = false; muzzle.Play(); SendMessageUpwards("Recoil"); if (Physics.Raycast(camera.transform.position, camera.transform.forward, out hit)) { print("sniper active"); if (hit.collider.gameObject.CompareTag("enemy")) { //muzzle.Play(); print("sniper shot taken"); hbScript.heathManager(100); if (hbScript.health <= 0f) { Destroy(hit.transform.gameObject); } } } StartCoroutine(TimeDelay(5)); } } } else if (gunSwitch == 2) { //ArRifle.SetActive(false); Sniper.gameObject.SetActive(false); print("shotty active"); daShotgun.yesWork = true; //this.GetComponent<shotty>().enabled = true; } else if (gunSwitch == 3) { //ArRifle.SetActive(false); daShotgun.yesWork = false; Pistol.SetActive(true); RaycastHit hit; if (Input.GetMouseButtonDown(0)) { GunSound.PlayOneShot(AR); muzzle.Play(); SendMessageUpwards("Recoil"); if (Physics.Raycast(camera.transform.position, camera.transform.forward, out hit)) { print("pistol active"); if (pistolshotbool == true) { pistolshotNumber++; if (hit.collider.gameObject.CompareTag("enemy")) { if (hit.collider.GetType() == typeof(BoxCollider)) { hbScript.heathManager(25); if (hbScript.health <= 0f) { Destroy(hit.transform.gameObject); } } if (hit.collider.GetType() == typeof(SphereCollider)) { hbScript.heathManager(50); if (hbScript.health <= 0f) { Destroy(hit.transform.gameObject); } } } if (pistolshotNumber >= 11) { pistolshotbool = false; pistolshotNumber = 0; StartCoroutine(TimeDelay(3)); } } } } } else if (gunSwitch >= 4) { gunSwitch = 0; Pistol.SetActive(false); } if (Input.GetKeyDown(KeyCode.Q)) { gunSwitch++; } }