using UnityEngine;
using TMPro;
public class ProjectileGun : MonoBehaviour
{
public bool shootingEnabled = true;
[Header("Attatch your bullet prefab")]
public GameObject bullet;
//Gun stats
public float shootForce, upwardForce;
public float timeBetweenShooting, spread, reloadTime, timeBetweenShots;
public int magazineSize, bulletsPerTap;
public bool allowButtonHold;
int bulletsLeft, bulletsShot;
//recoil
public Rigidbody playerRb;
public float recoilForce;
//some bools
bool shooting, readyToShoot, reloading;
public Camera fpsCam;
public GameObject muzzleFlash;
public Transform attackPoint;
//Show bullet amount
//public CamShake camShake;
//public float camShakeMagnitude, camShakeDuration;
public TextMeshProUGUI text;
public bool allowInvoke = true;
private void Start()
{
bulletsLeft = magazineSize;
readyToShoot = true;
}
void Update()
{
MyInput();
//Set Text
text.SetText(bulletsLeft / bulletsPerTap + " / " + magazineSize / bulletsPerTap);
}
private void MyInput()
{
//Input
if (allowButtonHold) shooting = Input.GetKey(KeyCode.Mouse0);
else shooting = Input.GetKeyDown(KeyCode.Mouse0);
if (Input.GetKeyDown(KeyCode.R) && bulletsLeft < magazineSize && !reloading) Reload();
//Shoot
if (readyToShoot && shooting && !reloading && bulletsLeft > 0){
bulletsShot = bulletsPerTap;
Shoot(); //Function has to be after bulletsShot = bulletsPerTap
}
}
private void Shoot()
{
if (!shootingEnabled) return;
readyToShoot = false;
//Find the hit position using a raycast
Ray ray = fpsCam.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
RaycastHit hit;
//Check if the ray hits something
Vector3 targetPoint;
if (Physics.Raycast(ray, out hit))
targetPoint = hit.point;
else
targetPoint = ray.GetPoint(75);
//Calculate direction
Vector3 directionWithoutSpread = targetPoint - attackPoint.position;
//Spread
float x = Random.Range(-spread, spread);
float y = Random.Range(-spread, spread);
float z = Random.Range(-spread, spread);
//Calc Direction with Spread
Vector3 directionWithSpread = directionWithoutSpread + new Vector3(x, y, z);
//Instantiate bullet/projectile
GameObject currentBullet = Instantiate(bullet, attackPoint.position, Quaternion.identity);
currentBullet.transform.forward = directionWithSpread.normalized;
//AddForce
currentBullet.GetComponent<Rigidbody>().AddForce(directionWithSpread.normalized * shootForce, ForceMode.Impulse);
currentBullet.GetComponent<Rigidbody>().AddForce(fpsCam.transform.up * upwardForce, ForceMode.Impulse);
//Activate bullet
if (currentBullet.GetComponent<CustomProjectiles>()) currentBullet.GetComponent<CustomProjectiles>().activated = true;
Instantiate(muzzleFlash, attackPoint.position, Quaternion.identity);
//Shake Camera
//camShake.StartCoroutine(camShake.Shake(camShakeDuration, camShakeMagnitude));
bulletsLeft--;
bulletsShot--;
if (allowInvoke)
{
Invoke("ShotReset", timeBetweenShooting);
allowInvoke = false;
//Add recoil force to player (should only be called once)
playerRb.AddForce(-directionWithSpread.normalized * recoilForce, ForceMode.Impulse);
}
if (bulletsShot > 0 && bulletsLeft > 0)
Invoke("Shoot", timeBetweenShots);
}
private void ShotReset()
{
readyToShoot = true;
allowInvoke = true;
}
private void Reload()
{
reloading = true;
Invoke("ReloadingFinished", reloadTime);
}
private void ReloadingFinished()
{
bulletsLeft = magazineSize;
reloading = false;
}
#region Setters
public void SetShootForce(float v)
{
shootForce = v;
}
public void SetUpwardForce(float v)
{
upwardForce = v;
}
public void SetFireRate(float v)
{
float _v = 2 / v;
timeBetweenShooting = _v;
}
public void SetSpread(float v)
{
spread = v;
}
public void SetMagazinSize (float v)
{
int _v = Mathf.RoundToInt(v);
magazineSize = _v;
}
public void SetBulletsPerTap(float v)
{
int _v = Mathf.RoundToInt(v);
bulletsPerTap = _v;
}
#endregion
}
How to report your problem productively in the Unity3D forums:
How to understand compiler and other errors and even fix them yourself:
If you post a code snippet, ALWAYS USE CODE TAGS:
How to use code tags: Using code tags properly
Use code tags:
Ok thank you I will repost it now
Excellent.
To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.
Doing this should help you answer these types of questions:
- is this code even running? which parts are running? how often does it run? what order does it run in?
- what are the values of the variables involved? Are they initialized? Are the values reasonable?
- are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)
Knowing this information will help you reason about the behavior you are seeing.
You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene
You could also just display various important quantities in UI Text elements to watch them change as you play the game.
If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.
Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong: