So I’m working on my 3rd prototype section for my mechanics for my game idea and currently working on a gun that raycasts a bullet hole when it fires at an object however if the object is not the right prop (The game is based around hunters and props. The hunters shoot the props to win the game) then it will deduct 10 seconds off of the game timer. This is currently working however when the seconds on the game timer are below 10 seconds e.g. 01:06
If the hunter shoots an incorrect prop the timer should next say 00:56 however instead it simply resets the 59 back to 0 now I know that is referring to the top of my update method in which a if statement detects if the seconds is less than or equal to 0 it will then set it to 59 seconds however I don’t know how to make it take into account the previous time penalty so that displays those extra lost seconds from incorrectly hitting the wrong prop.
My other bugs I have is during the reloading of the gun currently at the moment I have it working for if you shoot 5 bullets out of a clip of 30 and reload it will set your ammo back to 30 and subtract 5 from the total ammo. However if you decide to empty an entire clip of ammo of 30 and then reload via the automatic reload then it will subtract 5 bullets from the total ammo before then firing the remaining 30 bullets in the full clip which then leads onto my final bug in which bullets in clip = 0 and the total ammo = 0 then for some reason it will reload 30 bullets from out of nowhere and then place the total ammo count into the negative.
I have included videos of each bug in action. and is posted below.
My code is as follows:
public class BulletHoles : MonoBehaviour
{
public GameObject t_bullet;
public GameObject prop;
private float nextFire = 0.0f;
private float fireRate = 0.5f;
//public AudioClip gunShot;
//public AudioClip reload;
public int bp_Clip = 30;
public int t_Ammo = 90;
public int r_Ammo = 30;
public int tmp_Ammo = 0;
public float t_Left;
public Text timerText;
public Text timer;
public Text m_Winner;
public float min = 9.0f;
public float sec = 59.0f;
public float maxSec = 59.0f;
public float tmp_Sec = 0.0f;
public float tmp_SecTwo = 0.0f;
public float delay_Sec = 10.0f;
public Text ammo;
// Update is called once per frame
void Update()
{
if (sec <= 0)
{
sec = 59.0f;
if (min >= 1)
{
min--;
}
else
{
min = 0;
sec = 0;
m_Winner.text = "Props Win";
}
}
else
{
sec -= Time.deltaTime;
}
timerText.text = "Time Left: ";
timer.text = min.ToString("00") + ":" + sec.ToString("00");
ammo.text = bp_Clip.ToString("00") + "/" + t_Ammo.ToString("00");
if (bp_Clip > 30)
{
bp_Clip = 30;
}
if (t_Ammo == 0)
{
t_Ammo = 0;
}
Fire();
if (Input.GetKeyDown(KeyCode.R)) // this 1 works
{
if (bp_Clip <30)
{
StartCoroutine(Reload());
}
}
if (bp_Clip < 1) // this 1 subtracts ammo from total ammo for 2-3 bullets before
// subtracting from clip
{
StartCoroutine(Reload());
}
}
void Fire()
{
if (bp_Clip > 0)
{
if (Input.GetButton("Fire1") && Time.time > nextFire)
{
nextFire = Time.time + fireRate;
// AudioSource.PlayClipAtPoint(gunShot, transform.position, 1);
ForceFire();
bp_Clip -= 1;
}
}
}
// Now reloads properly however if total ammo is at 0 it still takes extra ammo and pushes the total in
// to negative figures
// also if clip is full empty and then reloads it takes off some ammo from the total before then taking out of the
// clip
IEnumerator Reload()
{
Debug.Log("Reload Two");
//AudioSource.PlayClipAtPoint(reload, transform.position, 1);
yield return new WaitForSeconds(3.0f);
tmp_Ammo = r_Ammo - bp_Clip;
t_Ammo = t_Ammo - tmp_Ammo;
bp_Clip = bp_Clip + tmp_Ammo;
}
void ForceFire ()
{
Vector3 fwd = transform.TransformDirection(Vector3.forward);
RaycastHit hit;
Debug.DrawRay(transform.position, fwd * 10, Color.green);
if (Input.GetButton("Fire1"))
{
if (Physics.Raycast(transform.position, fwd, out hit))
{
Instantiate(t_bullet, hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal));
if (hit.collider.tag == "Prop")
{
m_Winner.text = "Hunters Win";
Time.timeScale = 0; //Level Timer is stopped at whatever time it is at.
}
else if (hit.collider.tag != "Prop") // If hunter hits incorrect prop, subtract 10 seconds from level timer.
{
sec -= 10.0f;
if (sec < 10.0f && min > 1)
{
tmp_Sec = maxSec - sec;
sec = 0.0f;
sec = sec - tmp_Sec;
}
// tmp_Sec = maxSec - sec;
// tmp_SecTwo = maxSec - tmp_Sec;
//sec = sec - tmp_SecTwo;
//min -= 1;
// sec = sec - tmp_Sec;
// // if (sec <= 59.0f)
// {
// sec = sec - tmp_Sec;
// }
if (sec < 10.0f && min < 1) //This part works and makes it so time doesn't go into negative. If shot incorrect prop.
{
sec = 0.0f;
}
// Current bug - If you shoot the incorrect prop when the timer is x:09 or any other second value under 10 it will not take into account
// the extra seconds being deducted when the second timer resets e.g. 9:05 - 10 seconds should equal 8:55 however it displays 8:59
}
}
}
}
}
Timer bug:
Reload bug 1:
Reload bug 2:
Anyone know what could be the solutions to the above problems?