- Home /
How can I match the score script to my fire script, on a per click basis
Hey,
My fire script uses Time.time to slow down how fast the user can click to fire the "Gun"
but my score script counts score based on how many time the mouse is clicked.
so even though I spam the mouse click the bullet only fires every 2 seconds
but the score counts up per click.
Fire Script:
if (Input.GetButton ("Fire1") && Time.time > nextFire)
{
nextFire = Time.time + fireRate;
var clone = Instantiate (projectile, transform.position, transform.rotation);
clone.rigidbody.AddForce(bulletSpawnPoint.transform.forward*shootForce);
}
Score Script:
function Update ()
{
if(Input.GetButtonDown("Fire1"))
{
score += -10;
UpdateScore();
}
if(score == 0)
{
Application.LoadLevel(2);
}
if (GameObject.Find("Enemy Mech")== 0)
{
Application.LoadLevel(3);
}
}
Answer by laradov · Nov 30, 2012 at 01:40 PM
It is because you are increasing score in
if(Input.GetButtonDown("Fire1"))
which means every mouse click. You should either use same if statement in Score Script Update :
if (Input.GetButton ("Fire1") && Time.time > nextFire)
or communicate between these scripts to increase score variable in Score Script from if statement in Fire Script. To do that ,as far as I know, define score variable and UpdateScore() function as static and in Fire Script:
if (Input.GetButton ("Fire1") && Time.time > nextFire)
{
nextFire = Time.time + fireRate;
var clone = Instantiate (projectile, transform.position, transform.rotation);
clone.rigidbody.AddForce(bulletSpawnPoint.transform.forward*shootForce);
Score.score += -10;
Score.UpdateScore();
}
You are welcome. If you mark the question as answered, other people may benefit.