//Attack is called when the weapon is used public override void Attack(WeaponAimInfo weaponAimInfo, GameObject meleeGameObject, GameObject prefabAttackLight, Transform transformHead, bool buttonDown) { //Reset attack interval to prevent continuous melee attacks m_attackIntervalTimer = m_template.GetAttackInterval(); if (weaponAimInfo.m_raycastHit) { //Weapon hit an object Debug.Log("Attacking with melee weapon, hit " + weaponAimInfo.m_hitInfo.transform.name); GameObject goHit = weaponAimInfo.m_hitInfo.collider.gameObject; //Apply random attack damage (between set min and max) to hit enemies if (goHit.CompareTag("Enemy")) { int damageAmount = Random.Range(m_template.GetMinAttackDamage(), m_template.GetMaxAttackDamage() + 1); weaponAimInfo.m_hitInfo.transform.GetComponent <Enemy>().Damage(damageAmount); UIManager.instance.ShowEnemyHitPopup(damageAmount, weaponAimInfo.m_hitInfo.point); } //Explode any hit objects with the ExplodeOnImpact component else if (goHit.CompareTag("ExplodeOnImpact")) { goHit.GetComponent <ExplodeOnImpact>().Explode(); } } else { Debug.Log("Attacking with melee weapon, hit nothing"); } //Trigger the attack animation and sound meleeGameObject.transform.Find("Weapon").GetComponent <Animator>().SetTrigger("Attack"); SoundEffectPlayer.instance.PlaySoundEffect2D(m_template.m_attackSound, m_template.m_attackSoundVolume, 0.95f, 1.05f); }
//Attack is called when the player shoots public override void Attack(WeaponAimInfo weaponAimInfo, GameObject gunGameObject, GameObject prefabFireLight, Transform transformHead, bool buttonDown) { if (!m_gunTemplate.GetContinuousFire() && !buttonDown) { //If continuous fire is disabled and the mouse button was not pressed on this frame, do not fire return; } //Reset attack interval to prevent continuous firing m_attackIntervalTimer = m_template.GetAttackInterval(); //Instantiate fire particles and light GameObject fireParticles = m_gunTemplate.GetFireParticles(); Transform parentMuzzle = gunGameObject.transform.Find("AimPoint"); if (fireParticles != null) { Object.Instantiate(fireParticles, parentMuzzle); } Object.Instantiate(prefabFireLight, parentMuzzle); if (weaponAimInfo.m_raycastHit) { //Gun hit an object Debug.Log("Gun fired, hit " + weaponAimInfo.m_hitInfo.transform.name); GameObject goHit = weaponAimInfo.m_hitInfo.collider.gameObject; //Apply random attack damage (between set min and max) to hit enemies if (goHit.CompareTag("Enemy")) { int damageAmount = Random.Range(m_template.GetMinAttackDamage(), m_template.GetMaxAttackDamage() + 1); weaponAimInfo.m_hitInfo.transform.GetComponent <Enemy>().Damage(damageAmount); UIManager.instance.ShowEnemyHitPopup(damageAmount, weaponAimInfo.m_hitInfo.point); } //Explode any hit objects with the ExplodeOnImpact component else if (goHit.CompareTag("ExplodeOnImpact")) { goHit.GetComponent <ExplodeOnImpact>().Explode(); } } else { Debug.Log("Gun fired, hit nothing"); } //Reduce ammo, or throw an error if shooting with no ammo if (m_loadedAmmo >= 0) { m_loadedAmmo--; } else { Debug.LogError("Shooting gun with no ammo (" + m_template.GetWeaponName() + ")"); } //Trigger the shoot animation and sound gunGameObject.transform.Find("Gun").GetComponent <Animator>().SetTrigger("Shoot"); SoundEffectPlayer.instance.PlaySoundEffect2D(m_template.m_attackSound, m_template.m_attackSoundVolume, 0.95f, 1.05f); }
void Update() { //Aim info to use for this frame weaponAimInfo = GetWeaponAimInfo(); //Call Update on all weapons each frame for (int i = 0; i < availableWeapons.Length; i++) { availableWeapons[i].Update(); } //Call HeldUpdate only on the weapon that is currently being held if (activeWeapon != null) { activeWeapon.HeldUpdate(); } //Player specific updates if (playerControlsWeapons) { CheckForWeaponSwitchInput(); CheckForAttackInput(); UpdateWeaponPosition(); UpdateCamera(); } //Hide the active weapon if the entity should have an empty hand if (emptyHand) { goWeapon.SetActive(false); } //Aiming down sights is activated by pressing the right mouse button with a gun or prototype weapon if ((activeWeapon is GunWeapon || activeWeapon is PrototypeWeapon) && Input.GetButton("Fire2")) { aimDownSights = true; } else { aimDownSights = false; } //Turn the primary weapon torch on/off if the L key is pressed if (activeWeapon is GunWeapon activeGun && Input.GetKeyDown(KeyCode.L)) { bool torchOn = activeGun.ToggleTorchOn(); EnableWeaponTorchGameObject(torchOn); } }
//Attack is called when the grenade is thrown public override void Attack(WeaponAimInfo weaponAimInfo, GameObject weaponGameObject, GameObject prefabAttackLight, Transform transformHead, bool buttonDown) { //Use up a grenade and reset attack interval to prevent continuous throwing m_grenadeCount--; m_attackIntervalTimer = m_template.GetAttackInterval(); //Hide the held grenade to give the appearance of it being thrown SetHideHeldWeapon(true); m_weaponHolder.SetHeldWeaponHidden(m_hideHeldWeapon); //Add a new ThrownGrenade - thrown grenades are handled seperately to allow multiple thrown grenades to be active at any time, // while still only having a single grenade slot in the weapon holder m_thrownGrenades.Add(new ThrownGrenade(this, m_grenadeTemplate.GetDelay(), transformHead)); //Play the throw sound set in the template editor SoundEffectPlayer.instance.PlaySoundEffect2D(m_grenadeTemplate.m_throwSound, m_grenadeTemplate.m_throwSoundVolume, 0.95f, 1.05f); }
//Alternate melee attack for guns public override void AlternateAttack(WeaponAimInfo weaponAimInfo, GameObject weaponGameObject, Transform transformHead) { //Alternate attack only allowed for guns with melee ability if (!m_gunTemplate.GetCanUseAsMelee()) { return; } //Reset attack interval to prevent continuous melee attacks m_alternateAttackIntervalTimer = m_gunTemplate.GetMeleeInterval(); if (weaponAimInfo.m_raycastHit) { //Gun hit an object Debug.Log("Attacking with gun as melee weapon, hit " + weaponAimInfo.m_hitInfo.transform.name); GameObject goHit = weaponAimInfo.m_hitInfo.collider.gameObject; //Apply random melee attack damage (between set min and max) to hit enemies if (goHit.CompareTag("Enemy")) { int damageAmount = Random.Range(m_gunTemplate.GetMinMeleeAttackDamage(), m_gunTemplate.GetMaxMeleeAttackDamage() + 1); weaponAimInfo.m_hitInfo.transform.GetComponent <Enemy>().Damage(damageAmount); UIManager.instance.ShowEnemyHitPopup(damageAmount, weaponAimInfo.m_hitInfo.point); } //Explode any hit objects with the ExplodeOnImpact component else if (goHit.CompareTag("ExplodeOnImpact")) { goHit.GetComponent <ExplodeOnImpact>().Explode(); } } else { Debug.Log("Attacking with gun as melee weapon, hit nothing"); } //Trigger the melee animation and sound weaponGameObject.transform.Find("Gun").GetComponent <Animator>().SetTrigger("MeleeAttack"); SoundEffectPlayer.instance.PlaySoundEffect2D(m_gunTemplate.m_meleeSound, m_gunTemplate.m_meleeSoundVolume, 0.95f, 1.05f); }
//Grenade has no alternate attack public override void AlternateAttack(WeaponAimInfo weaponAimInfo, GameObject weaponGameObject, Transform transformHead) { }