// Update is called once per frame void Update() { if (equipped) { //draws line from gunEnd to center of screen RaycastHit hit; Vector3 rayOrigin = fpsCam.ViewportToWorldPoint(new Vector3(.5f, .5f, 0)); //shoot function, automatic weapons, nearly identical to semi-auto, see below if (automatic && Input.GetButton("Fire1") && (Time.time > nextFireTime)) { if (cur_clip > 0) { nextFireTime = Time.time + fireRate; cur_clip = cur_clip - 1; Vector2 off_spr = Random.insideUnitCircle.normalized * spread; off_x = off_spr.x; off_y = off_spr.y; Vector3 spr = Quaternion.AngleAxis(off_x, Vector3.right) * Quaternion.AngleAxis(off_y, Vector3.up) * fpsCam.transform.forward; if (Physics.Raycast(rayOrigin, spr, out hit, range)) { GameObject n_enemy = hit.collider.gameObject; IDamageable dmgScript = hit.collider.gameObject.GetComponent <EnemyHealth>(); if (dmgScript != null) { dmgScript.Damage(damage, hit.point); if (dmgScript.Health() <= (dmgScript.Threshold())) { dmgScript.SetNextThreshold(dmgScript.Health() - dmgScript.Threshold()); sm.Swap(n_enemy); // wm.SwapEquip(n_enemy); } } if (hit.rigidbody != null) { hit.rigidbody.AddForce(-hit.normal * 100f); } //debug, draws line from gun end to center of screen lineRenderer.SetPosition(0, gunEnd.position); lineRenderer.SetPosition(1, hit.point); } StartCoroutine(ShotEffect()); } else if (Time.time > nextFireTime) { //empty gun sound } } //shoot function semi-auto else if (Input.GetButtonDown("Fire1") && (Time.time > nextFireTime)) { if (cur_clip > 0) { //sets next time on clock when player can fire again nextFireTime = Time.time + fireRate; cur_clip = cur_clip - 1; //calculates spread on weapon, randomly picks point within circle with a radius of 'spread' Vector2 off_spr = Random.insideUnitCircle.normalized * spread; off_x = off_spr.x; off_y = off_spr.y; Vector3 spr = Quaternion.AngleAxis(off_x, Vector3.right) * Quaternion.AngleAxis(off_y, Vector3.up) * fpsCam.transform.forward; if (Physics.Raycast(rayOrigin, spr, out hit, range)) { GameObject n_enemy = hit.collider.gameObject; IDamageable dmgScript = hit.collider.gameObject.GetComponent <EnemyHealth>(); //checks if shot object is damageable if (dmgScript != null) { //hurt enemy for 'damage' dmgScript.Damage(damage, hit.point); //If enemy's health is <= threshold, possess, set new threshold for possessed enemy for later if (dmgScript.Health() <= (dmgScript.Threshold())) { dmgScript.SetNextThreshold(dmgScript.Health() - dmgScript.Threshold()); sm.Swap(n_enemy); // wm.SwapEquip(n_enemy); } } //shot effects physics based object if (hit.rigidbody != null) { hit.rigidbody.AddForce(-hit.normal * 100f); } lineRenderer.SetPosition(0, gunEnd.position); lineRenderer.SetPosition(1, hit.point); } StartCoroutine(ShotEffect()); } else if (Time.time > nextFireTime) { //empty gun sound } } //reloads if 'r' is pressed else if (Input.GetKeyDown("r") && cur_clip != clip) { Reload(); } //sets next available firetime after reload is called else if (reloading && Time.time > nextFireTime) { transform.Translate(0, 0, -2); reloading = !reloading; } } }
public void Fire() { if (equipped && pMenu.activeSelf == false && oMenu.activeSelf == false && goScreen.activeSelf == false && winScreen.activeSelf == false) { //draws line from gunEnd to center of screen RaycastHit hit; Vector3 rayOrigin = fpsCam.ViewportToWorldPoint(new Vector3(.5f, .5f, 0)); if (reloading && Time.time > nextFireTime) { transform.Translate(0, 0, -2); reloading = !reloading; } //shoot function, automatic weapons, nearly identical to semi-auto, see below else if (automatic && Input.GetButton("Fire1") && (Time.time > nextFireTime)) { if (cur_clip > 0) { nextFireTime = Time.time + fireRate; cur_clip = cur_clip - 1; Vector2 off_spr = Random.insideUnitCircle.normalized * cur_spr; off_x = off_spr.x; off_y = off_spr.y; Vector3 spr = Quaternion.AngleAxis(off_x, Vector3.right) * Quaternion.AngleAxis(off_y, Vector3.up) * fpsCam.transform.forward; //Debug.DrawRay(fpsCam.transform.position, spr * 100, Color.red, 5f); if (Physics.Raycast(rayOrigin, spr, out hit, range)) { GameObject n_enemy = hit.collider.gameObject; IDamageable dmgScript = hit.collider.gameObject.GetComponent <EnemyHealth>(); if (dmgScript != null) { n_enemy.GetComponent <EnemyPOV>().hit(); dmgScript.Damage(damage, hit.point); if (dmgScript.Health() <= (dmgScript.Threshold())) { //print("hit"); dmgScript.SetNextThreshold(dmgScript.Health() - dmgScript.Increment()); sm.Swap(n_enemy); } } if (hit.rigidbody != null) { hit.rigidbody.AddForce(-hit.normal * 100f); } //debug, draws line from gun end to center of screen lineRenderer.SetPosition(0, gunEnd.position); lineRenderer.SetPosition(1, hit.point); } StartCoroutine(ShotEffect(off_x, off_y)); } else if (Time.time > nextFireTime) { //empty gun sound } } } }
// Update is called once per frame void Update() { if (cur_spr > min_spr) { cur_spr = cur_spr - spr_de; if (cur_spr < 0) { cur_spr = 0; } } if (equipped && pMenu.activeSelf == false && oMenu.activeSelf == false && goScreen.activeSelf == false && winScreen.activeSelf == false) { //draws line from gunEnd to center of screen RaycastHit hit; Vector3 rayOrigin = fpsCam.ViewportToWorldPoint(new Vector3(.5f, .5f, 0)); if (reloading && Time.time > nextFireTime) { transform.Translate(0, 0, -2); reloading = !reloading; } //shoot function, automatic weapons, nearly identical to semi-auto, see below else if (automatic && Input.GetButton("Fire1") && (Time.time > nextFireTime)) { if (cur_clip > 0) { if ((Time.time - nextFireTime) < perfectFireTime) //remember to place in automatic { cur_spr = cur_spr + spr_in; if (cur_spr > spread) { cur_spr = spread; } print(cur_spr); } /* else * { * cur_spr = min_spr; * }*/ nextFireTime = Time.time + fireRate; cur_clip = cur_clip - 1; Vector2 off_spr = Random.insideUnitCircle.normalized * cur_spr; off_x = off_spr.x; off_y = off_spr.y; Vector3 spr = Quaternion.AngleAxis(off_x, Vector3.right) * Quaternion.AngleAxis(off_y, Vector3.up) * fpsCam.transform.forward; //Debug.DrawRay(fpsCam.transform.position, spr * 100, Color.red, 5f); if (Physics.Raycast(rayOrigin, spr, out hit, range)) { GameObject n_enemy = hit.collider.gameObject; IDamageable dmgScript = hit.collider.gameObject.GetComponent <EnemyHealth>(); if (dmgScript != null) { n_enemy.GetComponent <EnemyPOV>().hit(); dmgScript.Damage(damage, hit.point); if (dmgScript.Health() <= (dmgScript.Threshold())) { //print("hit"); dmgScript.SetNextThreshold(dmgScript.Health() - dmgScript.Increment()); sm.Swap(n_enemy); } } if (hit.rigidbody != null) { hit.rigidbody.AddForce(-hit.normal * 100f); } //debug, draws line from gun end to center of screen lineRenderer.SetPosition(0, gunEnd.position); lineRenderer.SetPosition(1, hit.point); } StartCoroutine(ShotEffect(off_x, off_y)); } else if (Time.time > nextFireTime) { //empty gun sound } } //shoot function semi-auto else if (Input.GetButtonDown("Fire1") && (Time.time > nextFireTime)) { if (cur_clip > 0) { if ((Time.time - nextFireTime) < perfectFireTime) //remember to place in automatic { cur_spr = cur_spr + spr_in; if (cur_spr > spread) { cur_spr = spread; } print(cur_spr); } //sets next time on clock when player can fire again nextFireTime = Time.time + fireRate; cur_clip = cur_clip - 1; //calculates spread on weapon, randomly picks point within circle with a radius of 'spread' Vector2 off_spr = Random.insideUnitCircle.normalized * cur_spr; off_x = off_spr.x; off_y = off_spr.y; Vector3 spr = Quaternion.AngleAxis(off_x, Vector3.right) * Quaternion.AngleAxis(off_y, Vector3.up) * fpsCam.transform.forward; if (Physics.Raycast(rayOrigin, spr, out hit, range)) { GameObject n_enemy = hit.collider.gameObject; IDamageable dmgScript = hit.collider.gameObject.GetComponent <EnemyHealth>(); //checks if shot object is damageable if (dmgScript != null) { //hurt enemy for 'damage' dmgScript.Damage(damage, hit.point); n_enemy.GetComponent <EnemyPOV>().hit(); //If enemy's health is <= threshold, possess, set new threshold for possessed enemy for later if (dmgScript.Health() <= (dmgScript.Threshold())) { dmgScript.SetNextThreshold(dmgScript.Health() - dmgScript.Increment()); sm.Swap(n_enemy); } } //shot effects physics based object if (hit.rigidbody != null) { hit.rigidbody.AddForce(-hit.normal * 100f); } lineRenderer.SetPosition(0, gunEnd.position); lineRenderer.SetPosition(1, hit.point); } StartCoroutine(ShotEffect(off_x, off_y)); } else if (Time.time > nextFireTime) { //empty gun sound } } //reloads if 'r' is pressed else if (Input.GetKeyDown("r") && cur_clip != clip) { Reload(); } //sets next available firetime after reload is called else if (reloading && Time.time > nextFireTime) { transform.Translate(0, 0, -2); reloading = !reloading; } } ammoCount.SetText(cur_clip + "/" + cur_reserve); }