Diwali_Rifle Code Implementation😁
Overview
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.
Class Breakdown
Variables
-
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.
Methods
1. EquipWeapon
-
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.
-
2. UnEquipWeapon
-
Purpose: Handles unequipping the weapon, resetting necessary variables.
-
Parameters:
-
weaponUserType: Specifies who is unequipping the weapon.
-
-
Key Actions:
-
Logs the unequip action for debugging.
-
3. Shoot
-
Purpose: Handles firing logic, supporting both automatic and single-shot modes.
-
Actions:
-
Calls
AutoShootcoroutine for rapid fire whenisAutoFireOnis true. -
Calls
SingleShootfor single-shot firing.
-
4. AutoShoot
-
Purpose: Implements rapid fire by repeatedly calling
SingleShootwith a short interval. -
Key Actions:
-
Iteratively calls
SingleShootwhile the trigger button is pressed.
-
5. SingleShoot
-
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.
-
6. MoveBullet
-
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.
-
7. GetWeaponData
-
Purpose: Retrieves the weapon data for the DiwaliRifle.
-
Returns: A
WeaponDataobject containing details like damage and range.
Key Features
Dynamic Firing Modes
-
Supports both automatic and single-shot firing mechanisms for versatile combat.
Realistic Effects
-
Muzzle flashes, bullet caps, and wall hit effects enhance the visual experience.
-
3D audio integration provides immersive sound feedback.
Damage Mechanics
-
Raycasting ensures precise damage application to enemies or players.
-
Projectiles visually simulate bullet movement in the game environment.
Projectile Pooling
-
Efficient resource management using a pool for projectiles, reducing runtime instantiation overhead.
Implementation Code
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;
}
}
Notes
-
Ensure
WeaponInventorycontains the necessary data for the Diwali_Rifle. -
Test both firing modes and verify damage application.
-
Adjust
bulletSpeedorautoAttackIntervalfor balancing.
Maze Escape
Navigate the maze, grab the diamond, survive the hunt!
| Status | Prototype |
| Author | Goutamraj |
| Genre | Action |
| Tags | Singleplayer, Top down shooter |
More posts
- Sword Weapon Code ImplementationMay 13, 2025
- New Sword Weapon and Dash abilityMay 11, 2025
- Hit Effects and Mini MapMay 04, 2025
- Player Movement Animations with Blend TreeApr 28, 2025
- Add Precision Aim for Player and Update Guard Enemy with Laser MechanicsApr 20, 2025
- Guard Enemy ImplementationApr 14, 2025
- Guard EnemyApr 10, 2025
- Enemy PoolApr 08, 2025
- Enemy Handling SystemApr 06, 2025

Leave a comment
Log in with itch.io to leave a comment.