Skip to content

Commit d571442

Browse files
authored
Add AI generated comments to PlayerShooting.cs
1 parent 5c13f56 commit d571442

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

scripts/3d/PlayerShooting.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,50 @@
1+
// Originally used in https://github.com/mike-forked/Shared_Skyro_Unity_3D_URP/blob/michal-flaska/Assets/PersonalAssets/Scripts/PlayerShooting.cs
2+
3+
// Setup Instructions:
4+
// 1. Attach this script to the player GameObject
5+
// 2. Assign a projectile prefab to the projectilePrefab field in the Inspector
6+
// 3. Create an empty GameObject as a child of the player to serve as the fire point and assign it to firePoint
7+
// 4. Position the fire point where projectiles should spawn (typically at weapon muzzle)
8+
9+
// Dependencies:
10+
// - Projectile prefab must have a Rigidbody component
11+
// - Fire point Transform must be properly positioned and oriented
12+
13+
// Controls:
14+
// - Left Mouse Button: Fire weapon (hold for automatic, click for semi-automatic)
15+
// - U Key: Reload weapon
16+
// - B Key: Toggle between semi-automatic and automatic fire modes
17+
18+
// Configuration:
19+
// - projectileSpeed: How fast projectiles travel (default: 20)
20+
// - maxAmmo: Maximum ammunition capacity (default: 10)
21+
// - reloadingSpeed: Time in seconds between each ammo unit reload (default: 0.5)
22+
// - fireRate: Minimum time between shots in automatic mode (default: 0.1)
23+
124
using System.Collections;
225
using UnityEngine;
326

427
public class PlayerShooting : MonoBehaviour
528
{
29+
// Projectile configuration
630
public GameObject projectilePrefab;
731
public Transform firePoint;
832
public float projectileSpeed = 20f;
33+
34+
// Ammo system
935
public int currentAmmo = 10;
1036
public int maxAmmo = 10;
1137
public bool isReloading = false;
1238
public float reloadingSpeed = 0.5f;
39+
40+
// Fire mode configuration
1341
public float fireRate = 0.1f;
1442
private bool isAutomatic = false;
1543
private float nextFireTime = 0f;
1644

1745
void Start()
1846
{
47+
// Ensure ammo starts at maximum capacity
1948
if (currentAmmo != maxAmmo)
2049
{
2150
currentAmmo = maxAmmo;
@@ -24,12 +53,14 @@ void Start()
2453

2554
void Update()
2655
{
56+
// Toggle fire mode between semi-automatic and automatic
2757
if (Input.GetKeyDown(KeyCode.B))
2858
{
2959
isAutomatic = !isAutomatic;
3060
Debug.Log("Fire mode: " + (isAutomatic ? "AUTO" : "SEMI"));
3161
}
3262

63+
// Handle automatic fire mode (hold to shoot)
3364
if (isAutomatic)
3465
{
3566
if (Input.GetMouseButton(0) && Time.time >= nextFireTime)
@@ -38,6 +69,7 @@ void Update()
3869
nextFireTime = Time.time + fireRate;
3970
}
4071
}
72+
// Handle semi-automatic fire mode (click to shoot)
4173
else
4274
{
4375
if (Input.GetMouseButtonDown(0))
@@ -46,31 +78,40 @@ void Update()
4678
}
4779
}
4880

81+
// Initiate reload when U is pressed
4982
if (Input.GetKeyDown(KeyCode.U) && !isReloading)
5083
{
5184
StartCoroutine(Reload());
5285
}
5386
}
5487

88+
// Fires a projectile from the fire point if ammo is available and not reloading
5589
void Shoot()
5690
{
5791
if (currentAmmo > 0 && !isReloading)
5892
{
93+
// Instantiate projectile at fire point
5994
GameObject projectile = Instantiate(projectilePrefab, firePoint.position, firePoint.rotation);
6095

96+
// Apply velocity to projectile
6197
Rigidbody rb = projectile.GetComponent<Rigidbody>();
6298
rb.linearVelocity = firePoint.forward * projectileSpeed;
6399

100+
// Decrease ammo count
64101
currentAmmo--;
65102

103+
// Clean up projectile after 3 seconds
66104
Destroy(projectile, 3f);
67105
}
68106
}
69107

108+
// Reloads the weapon one bullet at a time until magazine is full
109+
70110
IEnumerator Reload()
71111
{
72112
isReloading = true;
73113

114+
// Reload ammunition one unit at a time
74115
while (currentAmmo < maxAmmo)
75116
{
76117
yield return new WaitForSeconds(reloadingSpeed);

0 commit comments

Comments
 (0)