- Home /
Need help on buget ammo count UI
If object enter trigger, - 1 counter counter = 20 when counter = 0 show "reload" for 4 secs then counter = 20
Comment
using System.Collections; using System.Collections.Generic; using UnityEngine;
namespace CodeMonkey.HealthSystemCM { public class GunController : MonoBehaviour { public bool isFiring; public BulletController bullet; private int shotsInSequense = 0; private bool isReloading = false;
public float bulletSpeed;
private float shotTimeCounter = 0.0f;
public float timeBetweenShots;
private float shotCounter;
public Transform firePoint;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
shotTimeCounter += Time.deltaTime;
if (isReloading)
{
if (shotTimeCounter > 4.0f) //reload time 4S
{
isReloading = false;
shotTimeCounter = 0.0f;
shotsInSequense = 0;
}
}
else
{
if (isFiring)
{
if (shotTimeCounter > timeBetweenShots)
{
shotsInSequense++;
if (shotsInSequense > 20) isReloading = true; //6 shots then reload
else
{
shotTimeCounter = 0.0f;
BulletController newBullet = Instantiate(bullet, firePoint.position, firePoint.rotation) as BulletController;
newBullet.speed = bulletSpeed;
}
}
}
}
}
}
}
using System.Collections; using System.Collections.Generic; using UnityEngine;
namespace CodeMonkey.HealthSystemCM {
public class BulletController : MonoBehaviour
{
public float speed;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
transform.Translate(Vector3.forward * speed * Time.deltaTime);
Destroy(gameObject, 2);
}
private void OnTriggerEnter(Collider other)
{
if (other.TryGetComponent(out EnemyHealth Enemy))
{
Enemy.Damage();
// Destroy(gameObject);
}
}
}
}
Your answer
Follow this Question
Related Questions
how do i attach a gun to my character 2 Answers
GUI does not show my current ammo 0 Answers
Gun with ammo help 1 Answer
problem with ammo count 1 Answer
Ammo Pick problem im using c# 0 Answers