public virtual void Attack() { anim.SetTrigger("Attack"); anim.SetBool("Walking", false); playerHealth = playerPostion.transform.root.gameObject.GetComponent <HealthSinglePlayer>(); playerHealth.TakeDamage(attackDamage); attackTimer = attackTimerSet; }
void MeleeAttack(float damage, HealthSinglePlayer health) { if (attackTimer <= 0f) { Debug.Log("Attack!"); health.TakeDamage(damage); attackTimer = attackTimerSet; anim.SetTrigger("Attack"); } }
public override void Update() { base.Update(); if (canFireAuto) { currentMagazine--; anim.SetTrigger("Shoot"); anim.SetTrigger("Fire"); //Projectile if (isProjectile) { } //Raycast/Hitscan else { //Instantiating 3 rays Ray[] rays = new Ray[3]; Vector3 cameraRotationVector = cam.transform.rotation.eulerAngles; //Setting each ray to their respective orientation rays[1] = new Ray(cam.transform.position, cameraRotationVector); Vector3 tempVector = new Vector3(cameraRotationVector.x - threeShotSpread, cameraRotationVector.y, cameraRotationVector.z); rays[0] = new Ray(cam.transform.position, tempVector); tempVector = new Vector3(cameraRotationVector.x + threeShotSpread, cameraRotationVector.y, cameraRotationVector.z); rays[2] = new Ray(cam.transform.position, cameraRotationVector); for (int i = 0; i < 3; i++) { RaycastHit hit; if (Physics.Raycast(rays[i], out hit)) { if (hit.collider.CompareTag("Enemy Body")) { HealthSinglePlayer health = hit.collider.transform.root.GetComponent <HealthSinglePlayer>(); health.TakeDamage(damage); } else if (hit.collider.CompareTag("Enemy Head")) { HealthSinglePlayer health = hit.collider.transform.root.GetComponent <HealthSinglePlayer>(); health.TakeDamage(damage * critMultiplier); playerProperties.ShowHitmarker(true, damage * critMultiplier); } } } } } }
// Use this for initialization IEnumerator Melee() { quickMeleeCollider.enabled = true; yield return(new WaitForSeconds(meleeTime)); quickMeleeCollider.enabled = false; if (weaponSwitch.equippedWeapon != null) { weaponSwitch.equippedWeapon.SendMessage("QuickMelee", .5f); } if (hitEnemy) { enemyHealth.TakeDamage(playerProperties.playerStrength * damage); playerProperties.ShowHitmarker(false); enemyHealth = null; hitEnemy = false; } yield return(null); }
protected virtual void DoDamage() { health.TakeDamage(damage); }
// Update is called once per frame #region Update Function public override void Update() { base.Update(); if (isPickup || Time.timeScale == 0) { return; } if (!weaponEnabled) { return; } anim.SetFloat("Charge Speed", 1 / timeToFire); #region Setting Properties related To PlayerProperties if (playerProperties == null) { Debug.Log("No PlayerProperties attached"); } else { reloadSpeed = playerProperties.handSpeed; if (currentMagazine > magazineSize) { currentMagazine = magazineSize; } if (aiming) { aimingTimer = .25f / reloadSpeed; playerProperties.SetAiming(true); } else { playerProperties.SetAiming(false); } } #endregion #region Setting canSwitchWeapon if (reloadTimer > 0) { canSwitchWeapon = false; } else { if (aimingTimer > 0) { canSwitchWeapon = false; } else { canSwitchWeapon = true; } } #endregion #region Setting maxAngleMultiplier and FOV if (reloadTimer <= 0) { if (Input.GetKey(control.aim)) { maxAngleMultiplier = 1 / aimingDivider; aiming = true; if (cam.fieldOfView > (30 / zoomAmount)) { cam.fieldOfView -= 250 * Time.deltaTime; } } else { maxAngleMultiplier = 1; aiming = false; if (cam.fieldOfView < 60) { cam.fieldOfView += 250f * Time.deltaTime; } } } #endregion #region Setting gunAnim bool anim.SetBool("Aiming", aiming); #endregion #region Setting maxPelletSpreadAngle maxSpreadAngle = defaultAngle; maxSpreadAngle *= maxAngleMultiplier; #endregion #region Shooting #region Valdating the player meets the shoot requirements if (canFireSemiAuto || timeToFireTimer > 0) //Player presses and holds the shoot button, or they have already held the shoot button { if (timeToFireTimer >= timeToFire) { if (Input.GetKeyUp(control.fire))//Player releases the shoot button { StartRecoil(); currentMagazine -= 1f; //Using 1 ammo lastShot = Time.time; //Setting lastShot equal to current time timeToFireTimer = 0; RaycastHit hit; //Used to return hit data from when one of the rays hits a collider soundCounter = true; anim.SetTrigger("Fire"); anim.SetTrigger("Shoot"); anim.SetBool("Charging", false); Ray[] pellets = new Ray[numberOfPellets];//Creating an array of pellet rays that will be randomly fired in a specifically sized cone if (!isProjectile) { for (int i = 0; i < numberOfPellets; i++) //Filling the array of Rays { #region Creating a random Ray with an unchanging starting point within a cone Vector3 fireDirection = cam.transform.forward; Quaternion fireRotation = Quaternion.LookRotation(fireDirection); Quaternion randomRotation = Random.rotation; fireRotation = Quaternion.RotateTowards(fireRotation, randomRotation, Random.Range(0f, maxSpreadAngle)); #endregion pellets[i] = new Ray(cam.transform.position, fireRotation * Vector3.forward); } aud.PlayOneShot(attackSound); float damageCounter = 0; bool isHeadShot = false, isBodyShot = false, hitSomething = false; #region Seeing if rays hit a collider and doing stuff with it for (int i = 0; i < numberOfPellets; i++) { if (Physics.Raycast(pellets[i], out hit)) { #region Instantiating Hit Particle facing player var direction = cam.transform.position - hit.point; var tempParticle = Instantiate(hitParticle, hit.point, Quaternion.LookRotation(direction)); //Spawns a hit particle where the ray intersects with a collider Destroy(tempParticle, .5f); #endregion hitSomething = true; if (hit.collider.CompareTag("Enemy Body") || hit.collider.CompareTag("Enemy")) { #region If ray hits the body if (hit.collider.transform.parent.GetComponent <HealthSinglePlayer>())//Making sure the collider has a health object attached { isBodyShot = true; health = hit.collider.transform.parent.GetComponent <HealthSinglePlayer>(); health.TakeDamage(appliedDamage, !isSilenced); damageCounter += appliedDamage; } else if (hit.collider.transform.root.GetComponent <HealthSinglePlayer>())//Making sure the collider has a health object attached { isBodyShot = true; health = hit.collider.transform.root.GetComponent <HealthSinglePlayer>(); health.TakeDamage(appliedDamage, !isSilenced); damageCounter += appliedDamage; } else { Debug.Log("No HealthSinglePlayer component is in the parent transform!!");//Debug } #endregion } else if (hit.collider.CompareTag("Enemy Head")) { #region If ray hits the head if (hit.collider.transform.parent.GetComponent <HealthSinglePlayer>()) { isHeadShot = true; health = hit.collider.transform.parent.GetComponent <HealthSinglePlayer>(); health.TakeDamage(appliedDamage * critMultiplier, !isSilenced); damageCounter += appliedDamage * critMultiplier; } #endregion } } } if (isBodyShot && hitSomething) { playerProperties.ShowHitmarker(false, (int)damageCounter); } if (isHeadShot && hitSomething) { playerProperties.ShowHitmarker(true, (int)damageCounter); } #endregion } else { for (int i = 0; i < numberOfPellets; i++) //Filling the array of Rays { #region Creating a random Ray with an unchanging starting point within a cone Vector3 fireDirection = projectileSpawn.transform.forward; Quaternion fireRotation = Quaternion.LookRotation(fireDirection); Quaternion randomRotation = Random.rotation; fireRotation = Quaternion.RotateTowards(fireRotation, randomRotation, Random.Range(0f, maxSpreadAngle)); #endregion pellets[i] = new Ray(projectileSpawn.transform.position, fireRotation * Vector3.forward); } aud.PlayOneShot(attackSound); #region Spawning in Projectiles and Shooting for (int i = 0; i < numberOfPellets; i++) { GameObject tempProjectile = Instantiate(projectileToBeShot, pellets[i].origin, Quaternion.LookRotation(pellets[i].direction)); tempProjectile.GetComponent <Rigidbody>().AddForce(pellets[i].direction * appliedProjectileSpeed, ForceMode.Impulse); tempProjectile.SendMessage("SetDamage", appliedDamage); tempProjectile.GetComponent <Projectile>().damage = appliedDamage; tempProjectile.GetComponent <Projectile>().playerProperties = playerProperties; tempProjectile.GetComponent <Projectile>().critHitDamage = critMultiplier; if (tempProjectile.GetComponent <Projectile>().projectileType == Projectile.Type.Explosive) { ExplosiveProjectile tempExplosive = (ExplosiveProjectile)tempProjectile.GetComponent <Projectile>(); tempExplosive.blastRadius = 10; } } #endregion } } } else { timeToFireTimer += Time.deltaTime; appliedDamage += rampUpDamageAmount * Time.deltaTime; appliedProjectileSpeed += rampUpSpeedAmount * Time.deltaTime; if (Input.GetKeyUp(control.fire)) { aud.Stop(); Debug.Log("Released button"); if (fireEarly) { StartCoroutine("Recoil"); currentMagazine -= 1f; //Using 1 ammo lastShot = Time.time; //Setting lastShot equal to current time RaycastHit hit; //Used to return hit data from when one of the rays hits a collider soundCounter = true; anim.SetTrigger("Fire"); anim.SetTrigger("Shoot"); anim.SetBool("Charging", false); Ray[] pellets = new Ray[numberOfPellets];//Creating an array of pellet rays that will be randomly fired in a specifically sized cone if (!isProjectile) { for (int i = 0; i < numberOfPellets; i++) //Filling the array of Rays { #region Creating a random Ray with an unchanging starting point within a cone Vector3 fireDirection = cam.transform.forward; Quaternion fireRotation = Quaternion.LookRotation(fireDirection); Quaternion randomRotation = Random.rotation; fireRotation = Quaternion.RotateTowards(fireRotation, randomRotation, Random.Range(0f, maxSpreadAngle)); #endregion pellets[i] = new Ray(cam.transform.position, fireRotation * Vector3.forward); } aud.PlayOneShot(attackSound); float damageCounter = 0; bool isHeadShot = false, isBodyShot = false, hitSomething = false; #region Seeing if rays hit a collider and doing stuff with it for (int i = 0; i < numberOfPellets; i++) { if (Physics.Raycast(pellets[i], out hit)) { #region Instantiating Hit Particle facing player var direction = cam.transform.position - hit.point; var tempParticle = Instantiate(hitParticle, hit.point, Quaternion.LookRotation(direction)); //Spawns a hit particle where the ray intersects with a collider tempParticle.GetComponent <SpawnBulletHoleImage>().SetHitRotation(hit.normal); tempParticle.GetComponent <SpawnBulletHoleImage>().SetHit(hit); Destroy(tempParticle, .5f); #endregion hitSomething = true; if (hit.collider.CompareTag("Enemy Body") || hit.collider.CompareTag("Enemy")) { #region If ray hits the body if (hit.collider.transform.parent.GetComponent <HealthSinglePlayer>())//Making sure the collider has a health object attached { isBodyShot = true; health = hit.collider.transform.parent.GetComponent <HealthSinglePlayer>(); health.TakeDamage(appliedDamage); damageCounter += appliedDamage; } else if (hit.collider.transform.root.GetComponent <HealthSinglePlayer>())//Making sure the collider has a health object attached { isBodyShot = true; health = hit.collider.transform.root.GetComponent <HealthSinglePlayer>(); health.TakeDamage(appliedDamage); damageCounter += appliedDamage; } else { Debug.Log("No HealthSinglePlayer component is in the parent transform!!");//Debug } #endregion } else if (hit.collider.CompareTag("Enemy Head")) { #region If ray hits the head if (hit.collider.transform.parent.GetComponent <HealthSinglePlayer>()) { isHeadShot = true; health = hit.collider.transform.parent.GetComponent <HealthSinglePlayer>(); health.TakeDamage(appliedDamage * critMultiplier); damageCounter += appliedDamage * critMultiplier; } #endregion } } } if (isBodyShot && hitSomething) { playerProperties.ShowHitmarker(false, (int)damageCounter); } if (isHeadShot && hitSomething) { playerProperties.ShowHitmarker(true, (int)damageCounter); } #endregion } else { for (int i = 0; i < numberOfPellets; i++) //Filling the array of Rays { #region Creating a random Ray with an unchanging starting point within a cone Vector3 fireDirection = projectileSpawn.transform.forward; Quaternion fireRotation = Quaternion.LookRotation(fireDirection); Quaternion randomRotation = Random.rotation; fireRotation = Quaternion.RotateTowards(fireRotation, randomRotation, Random.Range(0f, maxSpreadAngle)); #endregion pellets[i] = new Ray(projectileSpawn.transform.position, fireRotation * Vector3.forward); } aud.PlayOneShot(attackSound); #region Spawning in Projectiles and Shooting for (int i = 0; i < numberOfPellets; i++) { GameObject tempProjectile = Instantiate(projectileToBeShot, pellets[i].origin, Quaternion.LookRotation(pellets[i].direction)); tempProjectile.GetComponent <Rigidbody>().AddForce(pellets[i].direction * appliedProjectileSpeed, ForceMode.Impulse); tempProjectile.SendMessage("SetDamage", appliedDamage); tempProjectile.SendMessage("SetBlastRadius", 10); tempProjectile.SendMessage("SetPlayerProperties", playerProperties); } #endregion } } else { anim.SetBool("Charging", false); } timeToFireTimer = 0; } Debug.Log("Holding down while less than charge time"); if (!aud.isPlaying) { if (soundCounter) { soundCounter = false; aud.PlayOneShot(chargeSound); } } anim.SetBool("Charging", true); } } else { Debug.Log("Player is not holding shoot button or the timer is 0"); appliedDamage = damage; appliedProjectileSpeed = projectileSpeed; timeToFireTimer = 0; anim.SetBool("Charging", false); } #endregion #region If Player Presses Reload Button if (!coroutinePlaying && currentMagazine != magazineSize && playerAmmo.GetAmount(ammoType) > 0) { if (Input.GetKeyDown(control.reload)) { Reload(false); } } #endregion #region Timers reloadTimer -= Time.deltaTime; aimingTimer -= Time.deltaTime; #endregion #region Setting Magazine Empty magazineEmpty = currentMagazine < 1; #endregion #region Calling reload If Magazine empty is true and asSoonAsEmpty is true if (magazineEmpty && asSoonAsMagEmpty) { Reload(false); } #endregion #region Updating Update Ammo and Weapon Switch updateAmmo.ChangeAmountObject(ammoAmount, currentMagazine); weaponSwitch.SetCanSwitchWeapon(canSwitchWeapon); #endregion }
// Update is called once per frame public override void Update() { base.Update(); if (isPickup) { return; } reloadSpeed = playerProperties.handSpeed; appliedReloadSpeed = reloadSpeed * reloadSpeedModifier; appliedADSSpeed = reloadSpeed * aimSpeed; if (currentMagazine > magazineSize) { currentMagazine = magazineSize; } #region Setting Animator Parameters anim.SetFloat("Reload Speed", appliedReloadSpeed); anim.SetFloat("ADS Speed", appliedADSSpeed); #endregion #region Setting Aiming if (aiming) { aimingTimer = .25f / reloadSpeed; playerProperties.SetAiming(true); } else { playerProperties.SetAiming(false); } #endregion #region Setting canSwitchWeapon if (reloadTimer > 0) { canSwitchWeapon = false; } else { if (aimingTimer > 0) { canSwitchWeapon = false; } else { canSwitchWeapon = true; } } #endregion #region Setting maxAngleMultiplier and FOV if (reloadTimer <= 0) { if (Input.GetKey(control.aim)) { maxAngleMultiplier = 1 / aimingDivider; aiming = true; if (cam.fieldOfView > (30 / zoomAmount)) { cam.fieldOfView -= 250 * Time.deltaTime; } } else { maxAngleMultiplier = 1; aiming = false; if (cam.fieldOfView < 60) { cam.fieldOfView += 250f * Time.deltaTime; } } } #endregion #region Setting gunAnim bool anim.SetBool("Aiming", aiming); #endregion #region Setting maxPelletSpreadAngle maxSpreadAngle = defaultAngle; maxSpreadAngle *= maxAngleMultiplier; maxSpreadAngle *= accuracyModifier; #endregion #region Shooting if (canFireSemiAuto) { lastShot = Time.time; //The player can shoot StartRecoil(); currentMagazine -= 1f; //Using 1 ammo RaycastHit hit; //Used to return hit data from when one of the rays hits a collider anim.SetTrigger("Fire"); anim.SetTrigger("Shoot"); Ray[] pellets = new Ray[numberOfPellets];//Creating an array of pellet rays that will be randomly fired in a specifically sized cone if (!isProjectile) { for (int i = 0; i < numberOfPellets; i++) //Filling the array of Rays { #region Creating a random Ray with an unchanging starting point within a cone Vector3 fireDirection = cam.transform.forward; Quaternion fireRotation = Quaternion.LookRotation(fireDirection); Quaternion randomRotation = Random.rotation; fireRotation = Quaternion.RotateTowards(fireRotation, randomRotation, Random.Range(0f, maxSpreadAngle)); #endregion pellets[i] = new Ray(cam.transform.position, fireRotation * Vector3.forward); } aud.PlayOneShot(attackSound); float damageCounter = 0; bool isHeadShot = false, isBodyShot = false, hitSomething = false; #region Seeing if rays hit a collider and doing stuff with it for (int i = 0; i < numberOfPellets; i++) { if (Physics.Raycast(pellets[i], out hit)) { #region Instantiating Hit Particle facing player var direction = cam.transform.position - hit.point; var tempParticle = Instantiate(hitParticle, hit.point, Quaternion.LookRotation(direction)); //Spawns a hit particle where the ray intersects with a collider tempParticle.GetComponent <SpawnBulletHoleImage>().SetHitRotation(hit.normal); tempParticle.GetComponent <SpawnBulletHoleImage>().SetHit(hit); Destroy(tempParticle, .5f); #endregion hitSomething = true; if (hit.collider.CompareTag("Enemy Body") /*|| hit.collider.CompareTag("Enemy")*/) { #region If ray hits the body if (hit.collider.transform.parent.GetComponent <HealthSinglePlayer>())//Making sure the collider has a health object attached { isBodyShot = true; health = hit.collider.transform.parent.GetComponent <HealthSinglePlayer>(); health.TakeDamage(damage, !isSilenced); damageCounter += damage; } else if (hit.collider.transform.root.GetComponent <HealthSinglePlayer>())//Making sure the collider has a health object attached { isBodyShot = true; health = hit.collider.transform.root.GetComponent <HealthSinglePlayer>(); health.TakeDamage(damage, !isSilenced); damageCounter += damage; } else { Debug.Log("No HealthSinglePlayer component is in the parent transform!!");//Debug } #endregion } else if (hit.collider.CompareTag("Enemy Head")) { #region If ray hits the head if (hit.collider.transform.parent.GetComponent <HealthSinglePlayer>()) { isHeadShot = true; health = hit.collider.transform.parent.GetComponent <HealthSinglePlayer>(); health.TakeDamage(damage * critMultiplier, !isSilenced); damageCounter += damage * critMultiplier; } else if (hit.collider.transform.root.GetComponent <HealthSinglePlayer>())//Making sure the collider has a health object attached { isHeadShot = true; health = hit.collider.transform.root.GetComponent <HealthSinglePlayer>(); health.TakeDamage(damage * critMultiplier, !isSilenced); damageCounter += damage * critMultiplier; } else { Debug.Log("No HealthSinglePlayer component is in the parent transform!!");//Debug } #endregion } } } if (isBodyShot && hitSomething) { playerProperties.ShowHitmarker(false, (int)damageCounter); } if (isHeadShot && hitSomething) { playerProperties.ShowHitmarker(true, (int)damageCounter); } #endregion } else { for (int i = 0; i < numberOfPellets; i++) //Filling the array of Rays { #region Creating a random Ray with an unchanging starting point within a cone Vector3 fireDirection = projectileSpawn.transform.forward; Quaternion fireRotation = Quaternion.LookRotation(fireDirection); Quaternion randomRotation = Random.rotation; fireRotation = Quaternion.RotateTowards(fireRotation, randomRotation, Random.Range(0f, maxSpreadAngle)); #endregion pellets[i] = new Ray(projectileSpawn.transform.position, fireRotation * Vector3.forward); } aud.PlayOneShot(attackSound); #region Spawning in Projectiles and Shooting for (int i = 0; i < numberOfPellets; i++) { GameObject tempProjectile = Instantiate(projectileToBeShot, pellets[i].origin, Quaternion.LookRotation(pellets[i].direction)); tempProjectile.GetComponent <Rigidbody>().AddForce(pellets[i].direction * 100); tempProjectile.SendMessage("SetDamage", damage); tempProjectile.SendMessage("SetPlayerProperties", playerProperties); } #endregion } } #endregion #region If Player Presses Reload Button if (reloadTimer <= 0) { if (Input.GetKeyDown(control.reload)) { Reload(); } } #endregion #region Timers reloadTimer -= Time.deltaTime; aimingTimer -= Time.deltaTime; #endregion #region Setting Magazine Empty magazineEmpty = currentMagazine < 1; #endregion #region Updating Update Ammo and Weapon Switch updateAmmo.ChangeAmountObject(ammoAmount, currentMagazine); weaponSwitch.SetCanSwitchWeapon(canSwitchWeapon); #endregion }
// Update is called once per frame public override void Update() { base.Update(); if (isPickup || !weaponEnabled) { return; } if (Time.timeScale == 0) { return; } reloadSpeed = playerProperties.handSpeed; appliedReloadSpeed = reloadSpeed * reloadSpeedModifier; appliedADSSpeed = reloadSpeed * aimSpeed; //If player is aiming, set the aiming timer relative to the reload speed if (aiming) { aimingTimer = .25f / appliedADSSpeed; //As reload speed increases, the aiming timer decreases, so it is equal to the animation speed playerProperties.SetAiming(true); anim.SetFloat("ADS Speed", appliedADSSpeed); anim.SetLayerWeight(2, .05f); } else { anim.SetLayerWeight(2, .2f); } //Sets canSwitchWeapon if (reloadTimer > 0) //If the player is still reloading { canSwitchWeapon = false; //They can't switch weapons } else { //The player is not reloading if (aimingTimer > 0) //But the player is either aiming, or the aiming animation is still playing { canSwitchWeapon = false; //They can't switch weapons } else//Not aiming or reloading { canSwitchWeapon = true;//They can switch weapons } } if (currentMagazine > magazineSize) { currentMagazine = magazineSize; } //Sets the angleMultiplier depending on whether or not the player is aiming if (reloadTimer <= 0) //If not reloading { if (Input.GetKey(control.aim)) //If user presses the aim button { maxAngleMultiplier = 1 / aimingDivider; aiming = true; //If the user is pressing the aim button, then aiming is true if (cam.fieldOfView > (30 / zoomAmount)) { cam.fieldOfView -= 250 * Time.deltaTime; } } else //The user isn't pressing the aim button { aiming = false; if (cam.fieldOfView < 60) { cam.fieldOfView += 250 * Time.deltaTime; } } } if (coroutinePlaying) { aiming = false; } //Plays the aiming animation depending on whether or not the player is aiming anim.SetBool("Aiming", aiming); maxSpreadAngle = defaultAngle; maxSpreadAngle *= maxAngleMultiplier; //Multiplies the maxBulletSpreadAngle by the multiplier, which is changed throughout the script depending on different things maxSpreadAngle *= accuracyModifier; //Actually Shooting #region Shooting if (canFireAuto) { currentMagazine -= 1f; //Subract one from the current gun magazine lastShot = Time.time; //lastShot is set equal to the current time, to compare in for the next shot RaycastHit hit; //Creates a raycasthit called hit anim.SetTrigger("Shoot");//Triggers the firing animation //vvvvvvvv Complicated math to create a random ray inside a cone pointing forward vvvvv Vector3 fireDirection = cam.transform.forward; Quaternion fireRotation = Quaternion.LookRotation(fireDirection); Quaternion randomRotation = Random.rotation; fireRotation = Quaternion.RotateTowards(fireRotation, randomRotation, Random.Range(0f, maxSpreadAngle)); //^^^^^^^ Complicated math to create a random ray inside a cone pointing forward ^^^^^^ //Creates a new ray originating at the cameras position, and pointing in the random direction that was created above Ray ray = new Ray(cam.transform.position, fireRotation * Vector3.forward); //Plays the gunShot sound once aud.PlayOneShot(attackSound); if (Physics.Raycast(ray, out hit)) //If the raycast hits a collider, returns information about the collider in a variable called hit { var direction = cam.transform.position - hit.point; //Sets a direction that faces the player var tempParticle = Instantiate(hitParticle, hit.point, Quaternion.LookRotation(direction)); //Sets tempParticle equal to an instantiated hit particle facing the player tempParticle.GetComponent <SpawnBulletHoleImage>().SetHitRotation(hit.normal); tempParticle.GetComponent <SpawnBulletHoleImage>().SetHit(hit); Destroy(tempParticle, 1); //Destroys the particle object after a second, to get rid of unnecessary objects if (hit.collider.CompareTag("Enemy Body")) //if the object hit is tagged with "Enemy Body" { if (hit.collider.transform.root.GetComponent <HealthSinglePlayer>()) //If the collider has a HealthSinglePlayer component attached { playerProperties.ShowHitmarker(false, (int)damage); health = hit.collider.transform.root.GetComponent <HealthSinglePlayer>(); //Sets the health to the health script attached to the gameObject hit health.TakeDamage(damage, !isSilenced); //Calls the takeDamage method using damage as the argument } } else if (hit.collider.CompareTag("Enemy Head")) //If the object hit is tagged with "Enemy head" { if (hit.collider.transform.root.GetComponent <HealthSinglePlayer>()) { playerProperties.ShowHitmarker(true, (int)(damage * critMultiplier)); health = hit.collider.transform.root.GetComponent <HealthSinglePlayer>(); //Same as before health.TakeDamage(damage * critMultiplier, !isSilenced); //Deals double damage } } } StartRecoil(); } #endregion UpdateReticle(); //Reload call when "R" is pressed if (!coroutinePlaying) //Not reloading a { if (Input.GetKeyDown(control.reload) && (currentMagazine != magazineSize && playerAmmo.GetAmount(ammoType) != 0)) //Player presses reload button { Reload(true); //Call the reload method } } //Sets the angleMultiplier if (!aiming) { //If the player isn't aiming. Angle multiplier is changed higher up if the player is aiming playerProperties.SetAiming(false); if (Input.GetKey(control.fire)) { //The fire button is being held down if (bulletSpreadTimer < 5) { //The bulletSpreadTimer is less than 5 (seconds) bulletSpreadTimer += Time.deltaTime; //Continue to increase the timer } if (bulletSpreadTimer < 6) { maxAngleMultiplier = angleSpreadAdder + bulletSpreadTimer; //Increase multiplier } } else { //The fire button isn't held down bulletSpreadTimer = 0; //Set the timer to 0 if (maxAngleMultiplier > 1) { maxAngleMultiplier -= Time.deltaTime; //Decrease the angleMultiplier over time } } } //Sets magazine empty to whether or not currentMagaine is less than 1 magazineEmpty = (currentMagazine < 1); if (magazineEmpty && asSoonAsMagEmpty && !coroutinePlaying) { Reload(true); } if (magazineEmpty) { anim.SetBool("Gun Empty", true); } else { anim.SetBool("Gun Empty", false); } //Decreasing general timers reloadTimer -= Time.deltaTime; aimingTimer -= Time.deltaTime; //Sending reloadSpeed float to the animator // armAnim.SetFloat("Reload Speed", reloadSpeed); anim.SetFloat("Reload Speed", appliedReloadSpeed); updateAmmo.ChangeAmountObject(ammoAmount, currentMagazine); weaponSwitch.SetCanSwitchWeapon(canSwitchWeapon); }
public void InitiallyHitEnemy(Collider other) { health = other.transform.root.GetComponent <HealthSinglePlayer>(); health.TakeDamage(damage); playerProperties.ShowHitmarker(false, (int)damage); }
public void StayingInEnemy(Collider other) { health = other.transform.root.GetComponent <HealthSinglePlayer>(); health.TakeDamage(stayingDamagePerSecond * Time.deltaTime); playerProperties.ShowHitmarker(false, false); }
// Update is called once per frame void FixedUpdate() { if (Input.GetKey(control.fire)) { if (currentMagazine > 0) { if (!coroutinePlaying) { if (!aud.isPlaying) { aud.PlayOneShot(attackSound); } particleRotation = particleSpawnPoint.transform.rotation; currentMagazine -= (Time.deltaTime * ammoUsePerSecond); anim.SetBool("Firing", true); #region Beam Is a Ray if (radiusOfBeam > 0) { RaycastHit hit; if (Physics.SphereCast(cam.transform.position, radiusOfBeam, cam.transform.forward, out hit, distance)) { RaycastHit particleHit; Physics.Raycast(cam.transform.position, cam.transform.forward, out particleHit); var direction = cam.transform.position - hit.point; var tempParticle = Instantiate(hitParticle, hit.point, Quaternion.LookRotation(direction)); //Spawns a hit particle where the ray intersects with a collider Destroy(tempParticle, .5f); particleRotation = Quaternion.LookRotation(particleHit.point - particleSpawnPoint.transform.position); Debug.Log("Capsule hit collider: " + hit.collider.name); if (hit.collider.CompareTag("Enemy") || hit.collider.CompareTag("Enemy Body") || hit.collider.CompareTag("Enemy Head")) { RaycastHit lineOfSight; if (Physics.Raycast(cam.transform.position, hit.transform.position - cam.transform.position, out lineOfSight)) { if (lineOfSight.collider.GetInstanceID() == hit.collider.GetInstanceID()) { if (hit.collider.transform.root.GetComponent <HealthSinglePlayer>()) { health = hit.collider.transform.root.GetComponent <HealthSinglePlayer>(); health.TakeDamage(damage * Time.deltaTime); if (lingeringDamagePerSecond > 0) { health.DamageOverTime(lingeringDamagePerSecond, lingeringDamageTime); } if (!hitmarkerCoroutineRunning) { StartCoroutine(ShowDPSHitmarker()); } totalDamage += damage * Time.deltaTime; } } } } } } #endregion #region Beam is a Cylinder else { RaycastHit hit; if (Physics.Raycast(cam.transform.position, cam.transform.forward, out hit, distance)) { var direction = cam.transform.position - hit.point; var tempParticle = Instantiate(hitParticle, hit.point, Quaternion.LookRotation(direction)); //Spawns a hit particle where the ray intersects with a collider Destroy(tempParticle, .5f); particleRotation = Quaternion.LookRotation(hit.point - particleSpawnPoint.transform.position); if (hit.collider.CompareTag("Enemy") || hit.collider.CompareTag("Enemy Body") || hit.collider.CompareTag("Enemy Head")) { if (hit.collider.transform.root.GetComponent <HealthSinglePlayer>()) { health = hit.collider.transform.root.GetComponent <HealthSinglePlayer>(); health.TakeDamage(damage * Time.deltaTime); if (lingeringDamagePerSecond > 0) { health.DamageOverTime(lingeringDamagePerSecond, lingeringDamageTime); } if (!hitmarkerCoroutineRunning) { StartCoroutine(ShowDPSHitmarker()); } totalDamage += damage * Time.deltaTime; } } } } #endregion isShooting = true; if (!particleCoroutineRunning) { StartCoroutine(SpawnParticle()); } } } else { Reload(); } } else { anim.SetBool("Firing", false); isShooting = false; } updateAmmo.ChangeAmountObject(ammoAmount, currentMagazine); }