- Home /
How to call functions from update function
Hello. I have written myself a gun script using snippets of code I have found on the internet. I have modified it so that firing and reloading are there own seperate function instead of being in the update function so I have more control over them but I have kept my inputs handled it the update function. I want to know how to call upon my fire and reload functions from my update function var prefabBullet:Transform; var shootForce:float; var shots : int = 0; var maxShots : int = 100; var timeBetweenShots = .02; var shootSound : AudioClip; var reloadSound : AudioClip;
function Update()
{
if(Input.GetButtonDown("Fire1") && shots < maxShots)
{
//Call fire function
}
else if (shots >= maxShots && Input.GetKeyDown(KeyCode.R))
{
//Call reload function
}
}
function Fire()
{
var instanceBullet = Instantiate(prefabBullet, transform.position, Quaternion.identity);
instanceBullet.rigidbody.AddForce(transform.forward * shootForce);
audio.PlayOneShot(shootSound);
animation.CrossFade("M60Fire");
shots++;
yield WaitForSeconds (timeBetweenShots);
//Call the fire function again
}
function Reload()
{
shots = 0;
animation.CrossFade("M60Reload");
}
Any help is appreciated. Thanks, Zac
Answer by Graham-Dunnett · Jul 07, 2013 at 09:37 AM
if(Input.GetButtonDown("Fire1") && shots < maxShots)
{
//Call fire function
Fire();
}
else if (shots >= maxShots && Input.GetKeyDown(KeyCode.R))
{
//Call reload function
Reload();
}
Your answer
Follow this Question
Related Questions
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Setting Scroll View Width GUILayout 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
How to make OnTriggerEnter and OnTriggerExit functions more than once? 1 Answer
list of pre defined functions like Update and OnTriggerEnter to use 2 Answers