2024-07-01 03:11:08 +00:00
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using TMPro;
|
2024-07-03 12:03:50 +00:00
|
|
|
using Unity.VisualScripting;
|
2024-07-01 03:11:08 +00:00
|
|
|
using UnityEngine;
|
2024-07-03 12:03:50 +00:00
|
|
|
using UnityEngine.PlayerLoop;
|
2024-07-01 03:11:08 +00:00
|
|
|
|
|
|
|
public class Gun : MonoBehaviour
|
|
|
|
{
|
2024-07-01 03:11:10 +00:00
|
|
|
public GameObject bullet;
|
|
|
|
|
2024-07-01 03:11:08 +00:00
|
|
|
/// <summary>
|
|
|
|
/// gun stats
|
|
|
|
/// </summary>
|
|
|
|
public int magSize;
|
|
|
|
public int damage;
|
2024-07-01 03:11:10 +00:00
|
|
|
public float timeBtwShots;
|
2024-07-01 03:11:08 +00:00
|
|
|
public float range;
|
|
|
|
public float reloadTime;
|
|
|
|
public int bulletsPerFire;
|
|
|
|
public bool automaticFire;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// gun status
|
|
|
|
/// </summary>
|
|
|
|
private bool reloading;
|
|
|
|
private bool shooting;
|
|
|
|
private bool readyToShot;
|
|
|
|
private int bulletsLeft;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Refrencing
|
|
|
|
/// </summary>
|
|
|
|
[SerializeField]
|
|
|
|
Camera fpsCam;
|
2024-07-01 03:11:10 +00:00
|
|
|
public Transform bulletSpawn;
|
2024-07-01 03:11:08 +00:00
|
|
|
public TextMeshProUGUI bulletCounter;
|
2024-07-01 03:11:10 +00:00
|
|
|
public bool allowInvoke = true;
|
|
|
|
|
|
|
|
[SerializeField]
|
|
|
|
private GameObject mag;
|
|
|
|
|
|
|
|
void Awake()
|
|
|
|
{
|
|
|
|
bulletsLeft = magSize;
|
|
|
|
readyToShot = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Shoot()
|
|
|
|
{
|
|
|
|
if (readyToShot && !reloading && bulletsLeft > 0)
|
|
|
|
{
|
|
|
|
Debug.Log("Bang");
|
|
|
|
Ray ray = fpsCam.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
|
|
|
|
RaycastHit hit;
|
|
|
|
Vector3 targetPoint;
|
|
|
|
if (Physics.Raycast(ray, out hit))
|
2024-07-02 02:42:09 +00:00
|
|
|
{
|
|
|
|
targetPoint = hit.point;
|
|
|
|
}
|
2024-07-01 03:11:10 +00:00
|
|
|
else
|
2024-07-02 02:42:09 +00:00
|
|
|
{
|
|
|
|
targetPoint = ray.GetPoint(150);
|
|
|
|
}
|
|
|
|
|
2024-07-01 03:11:10 +00:00
|
|
|
Vector3 direction = targetPoint - bulletSpawn.position;
|
|
|
|
GameObject currentBullet = Instantiate(
|
|
|
|
bullet,
|
|
|
|
bulletSpawn.position,
|
|
|
|
Quaternion.identity
|
|
|
|
);
|
2024-07-02 02:42:09 +00:00
|
|
|
currentBullet.GetComponent<BulletCollider>().damage = damage;
|
2024-07-01 03:11:10 +00:00
|
|
|
currentBullet.transform.forward = direction.normalized;
|
|
|
|
currentBullet
|
|
|
|
.GetComponent<Rigidbody>()
|
|
|
|
.AddForce(direction.normalized * range, ForceMode.Impulse);
|
|
|
|
bulletsLeft--;
|
|
|
|
if (allowInvoke)
|
|
|
|
{
|
|
|
|
Invoke("ResetShot", timeBtwShots);
|
|
|
|
allowInvoke = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Update()
|
|
|
|
{
|
2024-07-02 02:42:09 +00:00
|
|
|
if (bulletsLeft == 0)
|
2024-07-01 03:11:10 +00:00
|
|
|
{
|
|
|
|
Reload();
|
|
|
|
}
|
2024-07-03 12:03:50 +00:00
|
|
|
if(fpsCam==null)
|
|
|
|
{
|
|
|
|
fpsCam=GameObject.Find("MainCamera").GetComponent<Camera>();
|
|
|
|
}
|
2024-07-01 03:11:10 +00:00
|
|
|
}
|
2024-07-02 02:42:09 +00:00
|
|
|
|
2024-07-01 03:11:10 +00:00
|
|
|
private void ResetShot()
|
|
|
|
{
|
|
|
|
//Allow shooting and invoking again
|
|
|
|
readyToShot = true;
|
|
|
|
allowInvoke = true;
|
|
|
|
}
|
2024-07-01 03:11:08 +00:00
|
|
|
|
2024-07-01 03:11:10 +00:00
|
|
|
public void Reload()
|
|
|
|
{
|
|
|
|
if (bulletsLeft < magSize && !reloading)
|
|
|
|
{
|
|
|
|
Debug.Log("reload");
|
|
|
|
reloading = true;
|
|
|
|
readyToShot = false;
|
|
|
|
Invoke("Reloaded", reloadTime);
|
|
|
|
mag.SetActive(false);
|
|
|
|
}
|
2024-07-01 03:11:08 +00:00
|
|
|
}
|
2024-07-01 03:11:10 +00:00
|
|
|
|
|
|
|
void Reloaded()
|
2024-07-01 03:11:08 +00:00
|
|
|
{
|
2024-07-01 03:11:10 +00:00
|
|
|
reloading = false;
|
|
|
|
readyToShot = true;
|
|
|
|
bulletsLeft = magSize;
|
|
|
|
mag.SetActive(true);
|
2024-07-01 03:11:08 +00:00
|
|
|
}
|
2024-07-03 12:03:50 +00:00
|
|
|
|
2024-07-01 03:11:08 +00:00
|
|
|
}
|