Posted May 19, 2025 by Goutamraj
#Diwali_Rifle
The Diwali_Rifle class implements the IWeapon
interface, providing the functionality for a fully-automatic firearm weapon with a rapid-fire feature and projectile-based damage mechanics. This weapon supports both automatic and single-shot firing modes, with realistic visual and sound effects.
weaponManager
: Manages the equipped weapon and associated actions.
weaponData
: Stores details about the weapon .
bulletSpeed
: A constant value defining the speed of the bullet.
isAutoFireOn
: Boolean to toggle between automatic and single-shot firing modes.
boolGiveDamageOnRayCastHit
: Controls whether damage is applied upon a successful raycast hit.
Purpose: Prepares the DiwaliRifle for use by equipping it, initializing its data, and enabling raycast-based damage.
Parameters:
weaponManager
: Reference to the weapon manager.
weaponUserType
: Specifies who is equipping the weapon (e.g., player or enemy).
weaponSwitchType
: Specifies the type of weapon switch (e.g., swap or drop-pick).
Key Actions:
Replaces the current weapon with the DiwaliRifle.
Retrieves weapon data and enables raycast damage.
Purpose: Handles unequipping the weapon, resetting necessary variables.
Parameters:
weaponUserType
: Specifies who is unequipping the weapon.
Key Actions:
Logs the unequip action for debugging.
Purpose: Handles firing logic, supporting both automatic and single-shot modes.
Actions:
Calls AutoShoot
coroutine for rapid fire when isAutoFireOn
is true.
Calls SingleShoot
for single-shot firing.
Purpose: Implements rapid fire by repeatedly calling SingleShoot
with a short interval.
Key Actions:
Iteratively calls SingleShoot
while the trigger button is pressed.
Purpose: Fires a single bullet, playing audio, visuals, and managing projectile movement.
Key Actions:
Plays muzzle flash and firing sounds.
Spawns bullet caps and applies random force and torque for realism.
Uses raycasting to determine hits and applies damage.
Spawns projectiles for visual representation of bullets.
Purpose: Handles the movement of the projectile towards the hit point, including visuals.
Parameters:
bullet
: Transform of the projectile being moved.
hit
: RaycastHit data representing the hit target.
isFakeHit
(optional): Specifies if the hit is simulated.
Key Actions:
Moves the projectile along a trail towards the target.
Spawns a wall hit effect if the hit object is not an enemy or player.
Purpose: Retrieves the weapon data for the DiwaliRifle.
Returns: A WeaponData
object containing details like damage and range.
Supports both automatic and single-shot firing mechanisms for versatile combat.
Muzzle flashes, bullet caps, and wall hit effects enhance the visual experience.
3D audio integration provides immersive sound feedback.
Raycasting ensures precise damage application to enemies or players.
Projectiles visually simulate bullet movement in the game environment.
Efficient resource management using a pool for projectiles, reducing runtime instantiation overhead.
public class DiwaliRifle : IWeapon { IWeaponManager weaponManager; WeaponData weaponData; const float bulletSpeed = 170; bool isAutoFireOn = true; bool boolGiveDamageOnRayCastHit = false; public void EquipWeapon(IWeaponManager weaponManager, WeaponUserType weaponUserType, WeaponSwitchType weaponSwitchType) { this.weaponManager = weaponManager; weaponData = WeaponInventory.instance.weaponsData.Find(ele => ele.weaponName == WeaponName.DiwaliRifle); weaponManager.weaponHolder.ReplaceWeapon(weaponData, weaponSwitchType); Debug.Log("EquipWeapon: DiwaliRifle"); boolGiveDamageOnRayCastHit = true; } public void UnEquipWeapon(WeaponUserType weaponUserType) { Debug.Log("UnEquipWeapon: DiwaliRifle"); } public void Shoot() { if (isAutoFireOn) { weaponManager.StartCoroutine(AutoShoot()); } else { SingleShoot(); } } public IEnumerator AutoShoot() { while (weaponManager.isPressingTriggerButton) { SingleShoot(); yield return new WaitForSeconds(weaponData.autoAttackInterval); } } public void SingleShoot() { AudioManager2.instance.Play3DAudio(AudioType.RifleFiring, weaponManager.transform.position); List<Transform> bulletStartPoints = weaponManager.weaponHolder.GetBulletStartPoints(); foreach (Transform bulletStartPoint in bulletStartPoints) { weaponManager.weaponHolder.PlayMuzzleFlash(); GameObject cap = IWeaponManager.Instantiate(weaponData.projectileCap); cap.transform.position = weaponManager.weaponHolder.GetHandPosition(false).position + bulletStartPoint.forward / 2.5f; Rigidbody bulletCapRB = cap.GetComponent<Rigidbody>(); bulletCapRB.AddForce(weaponManager.weaponHolder.transform.right * 3, ForceMode.Impulse); Ray ray = new Ray(bulletStartPoint.position, bulletStartPoint.forward); RaycastHit hit; if (Physics.Raycast(ray, out hit, weaponData.maxDistanceCanCover, weaponData.layerMask)) { if (boolGiveDamageOnRayCastHit && hit.collider.CompareTag(Consts.enemyTagString)) { hit.collider.GetComponent<IEnemy>().TakeDamage(weaponData.damage, weaponManager.gameObject, hit.point); } } } } public WeaponData GetWeaponData() { return weaponData; } }
Ensure WeaponInventory
contains the necessary data for the Diwali_Rifle.
Test both firing modes and verify damage application.
Adjust bulletSpeed
or autoAttackInterval
for balancing.