using UnityEngine; public class SimpleGun : MonoBehaviour { [Header("Gun Settings")] public float damage = 25f; public float range = 100f; public float fireRate = 0.5f; public int maxAmmo = 30; public int currentAmmo; [Header("References")] public Camera fpsCam; public ParticleSystem muzzleFlash; private float nextTimeToFire = 0f; void Start() { currentAmmo = maxAmmo; // Find camera if not assigned if (fpsCam == null) { fpsCam = Camera.main; } // Create simple gun visuals CreateSimpleGunModel(); } void Update() { // Shoot on left mouse button if (Input.GetButton("Fire1") && Time.time >= nextTimeToFire) { nextTimeToFire = Time.time + 1f / fireRate; Shoot(); } // Reload on R key if (Input.GetKeyDown(KeyCode.R)) { Reload(); } } void CreateSimpleGunModel() { // Main body GameObject body = GameObject.CreatePrimitive(PrimitiveType.Cube); body.transform.SetParent(transform); body.transform.localPosition = new Vector3(0, -0.1f, 0.3f); body.transform.localScale = new Vector3(0.1f, 0.15f, 0.4f); body.transform.localRotation = Quaternion.identity; Destroy(body.GetComponent()); // Remove collider // Set color Renderer bodyRenderer = body.GetComponent(); bodyRenderer.material.color = new Color(0.2f, 0.2f, 0.2f); // Dark gray // Barrel GameObject barrel = GameObject.CreatePrimitive(PrimitiveType.Cylinder); barrel.transform.SetParent(transform); barrel.transform.localPosition = new Vector3(0, -0.05f, 0.6f); barrel.transform.localScale = new Vector3(0.03f, 0.15f, 0.03f); barrel.transform.localRotation = Quaternion.Euler(90, 0, 0); Destroy(barrel.GetComponent()); Renderer barrelRenderer = barrel.GetComponent(); barrelRenderer.material.color = new Color(0.1f, 0.1f, 0.1f); // Even darker // Handle GameObject handle = GameObject.CreatePrimitive(PrimitiveType.Cube); handle.transform.SetParent(transform); handle.transform.localPosition = new Vector3(0, -0.25f, 0.15f); handle.transform.localScale = new Vector3(0.08f, 0.2f, 0.1f); handle.transform.localRotation = Quaternion.Euler(15, 0, 0); Destroy(handle.GetComponent()); Renderer handleRenderer = handle.GetComponent(); handleRenderer.material.color = new Color(0.3f, 0.2f, 0.1f); // Brown } void Shoot() { if (currentAmmo <= 0) { Debug.Log("Out of ammo! Press R to reload."); return; } currentAmmo--; Debug.Log($"Shot fired! Ammo: {currentAmmo}/{maxAmmo}"); // Play muzzle flash if assigned if (muzzleFlash != null) { muzzleFlash.Play(); } // Raycast from camera center RaycastHit hit; if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range)) { Debug.Log($"Hit: {hit.transform.name}"); // You can add hit detection for enemies here later // Example: hit.transform.GetComponent()?.TakeDamage(damage); } } void Reload() { Debug.Log("Reloading..."); currentAmmo = maxAmmo; } void OnGUI() { // Simple ammo counter in bottom-right GUI.color = Color.white; GUI.Label(new Rect(Screen.width - 120, Screen.height - 40, 100, 30), $"Ammo: {currentAmmo}/{maxAmmo}", new GUIStyle() { fontSize = 20, normal = new GUIStyleState() { textColor = Color.white } }); } }