b0364a3dbf
commented abit then built the game
117 lines
2.6 KiB
C#
117 lines
2.6 KiB
C#
/*
|
|
*Author: Lin Hengrui Ryan
|
|
*Date:21/06/2024
|
|
*Description: to make a gun script for the enemy
|
|
*/
|
|
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
|
|
public class enemyGun : MonoBehaviour
|
|
{
|
|
public GameObject bullet;
|
|
[Header("gun stats")]
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public int magSize;
|
|
public int damage;
|
|
public float timeBtwShots;
|
|
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;
|
|
[Header("Refrencing")]
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
[SerializeField]
|
|
Transform fpsCam;
|
|
public Transform bulletSpawn;
|
|
|
|
public bool allowInvoke = true;
|
|
|
|
[SerializeField]
|
|
private GameObject mag;
|
|
public AudioSource source;
|
|
|
|
void Awake()
|
|
{
|
|
bulletsLeft = magSize;
|
|
readyToShot = true;
|
|
}
|
|
|
|
public void Shoot()
|
|
{
|
|
if (readyToShot && !reloading && bulletsLeft > 0)
|
|
{
|
|
Debug.Log("Bang");
|
|
readyToShot = false;
|
|
source.PlayOneShot(GameManager.instance.bang);
|
|
GameObject currentBullet = Instantiate(
|
|
bullet,
|
|
bulletSpawn.position,
|
|
Quaternion.identity
|
|
);
|
|
currentBullet.GetComponent<BulletCollider>().damage = damage;
|
|
currentBullet
|
|
.GetComponent<Rigidbody>()
|
|
.AddForce(transform.forward * range, ForceMode.Impulse);
|
|
bulletsLeft--;
|
|
if (allowInvoke)
|
|
{
|
|
Invoke("ResetShot", timeBtwShots);
|
|
allowInvoke = false;
|
|
}
|
|
}
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
if (bulletsLeft == 0)
|
|
{
|
|
Reload();
|
|
}
|
|
}
|
|
|
|
private void ResetShot()
|
|
{
|
|
//Allow shooting and invoking again
|
|
readyToShot = true;
|
|
allowInvoke = true;
|
|
}
|
|
|
|
public void Reload()
|
|
{
|
|
if (bulletsLeft < magSize && !reloading)
|
|
{
|
|
source.PlayOneShot(GameManager.instance.reload1);
|
|
Debug.Log("reload");
|
|
reloading = true;
|
|
readyToShot = false;
|
|
Invoke("Reloaded", reloadTime);
|
|
mag.SetActive(false);
|
|
}
|
|
}
|
|
|
|
void Reloaded()
|
|
{
|
|
source.PlayOneShot(GameManager.instance.reload2);
|
|
|
|
reloading = false;
|
|
readyToShot = true;
|
|
bulletsLeft = magSize;
|
|
mag.SetActive(true);
|
|
}
|
|
}
|