- Home /
reloading script problem
hi i want to reload when ever the player press "r" and the current bullets aren't 30 but it dose not work here is the script using System.Collections; using System.Collections.Generic; using TMPro; using UnityEngine; using UnityEngine.UI;
public class shootak47 : MonoBehaviour
{
public Transform wheretoshoot;
int range = 1000;
bool ishooting = false;
bool isreloading = false;
public int bullet = 30;
public int currentbullet;
const float fireDelay = 0.25f;
float delayTimer = 0;
public GameObject sprite;
enemyhealth health;
void Start()
{
currentbullet = bullet;
}
void Update()
{
if (delayTimer > 0)
{
delayTimer -= Time.deltaTime;
if (delayTimer < 0) delayTimer = 0;
}
if(Input.GetKeyDown(KeyCode.R))
{
if(currentbullet <= 29 && !isreloading)
{
isreloading = true;
StartCoroutine(delaybetweenshots());
isreloading = false;
}
}
if (Input.GetButton("Fire1"))
{
if (delayTimer == 0)
{
shooting();
delayTimer = fireDelay;
}
}
Debug.Log(currentbullet);
}
public void shooting ()
{
if (currentbullet == 0) return;
if (isreloading == true) return;
if(!ishooting)
{
RaycastHit Hit;
if (Physics.Raycast(wheretoshoot.position, transform.forward, out Hit, range))
{
Instantiate(sprite, Hit.point, Quaternion.LookRotation(Hit.normal));
health = Hit.transform.GetComponent<enemyhealth>();
if(health != null)
{
health.takedamge(20);
}
}
}
currentbullet--;
if(currentbullet == 0)
{
StartCoroutine(delaybetweenshots());
}
}
IEnumerator delaybetweenshots()
{
yield return new WaitForSeconds(2f);
currentbullet = bullet;
}
}
Comment
Your answer
Follow this Question
Related Questions
Gun - Ammo , reloading and UI problem. 1 Answer
reload script does not work when ammo is 16 2 Answers
Shooting and reloading mechanism problem. 1 Answer
Reload Ammo is not working 1 Answer