- Home /
Question by
cynicalwhiskey · Jun 24, 2014 at 04:36 AM ·
timerammo
Help with regenerating ammo
Im trying to have a script to slowly regenerate ammo 1 per timer loop.
var playerSpeed : float = 10;
var maxWidth : float = 7;
var photorp : GameObject;
var photspawn : Transform;
var maxBullets : int = 5;
var bulletCount : int = 5;
var bulletTimer : float = 0;
private var bulletAddVar : int = 1;
function Start () {
}
function Update ()
{
transform.position.z += Input.GetAxis("Horizontal") * -playerSpeed * Time.deltaTime;
if (transform.position.z > maxWidth)
transform.position.z = maxWidth;
if (transform.position.z < -maxWidth)
transform.position.z = -maxWidth;
if (Input.GetButtonDown("Jump"))
if (bulletCount > 0)
{
Instantiate(photorp, transform.position, transform.rotation);
bulletCount -= 1;
}
if (bulletCount < maxBullets)
bulletTimer += 1 * Time.deltaTime;
{
if (bulletTimer >= 10)
bulletCount += 1;
bulletTimer = 0;
}
}
Comment
Answer by chargedneuron · Jun 24, 2014 at 06:15 AM
At first glance here is what I see;
a) bulletTimer += 1* Time.deltaTime is the same as bulletTimer += Time.deltaTime; b) you might be missing a set of {} around if(bulletTimer >= 10)
if (bulletCount < maxBullets)
bulletTimer += Time.deltaTime;
{
if (bulletTimer >= 10)
{
bulletCount += 1;
bulletTimer = 0;
}
}
Answer by Jiro-Ng · Jun 24, 2014 at 08:46 AM
What you doing here is same as this.
if (bulletCount < maxBullets)
bulletTimer += 1 * Time.deltaTime;
if (bulletTimer >= 10)
bulletCount += 1;
bulletTimer = 0;
The bracket is put in wrong position. It should be look like this.
if (bulletCount < maxBullets)
{
bulletTimer += 1 * Time.deltaTime;
if (bulletTimer >= 10)
{
bulletCount += 1;
bulletTimer = 0;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613152350im_/https://answers.unity.com/themes/thub/images/avi.jpg)