Game Development Reference
In-Depth Information
muzzleFlash.transform.localRotation = r
Quaternion.AngleAxis(Random.value * 360, Vector3.forward);
muzzleFlash.enabled = true;
GameObject.Find(“MuzzleLight”).GetComponent(Light).enabled = true;
if (audio) {
if (!audio.isPlaying)
audio.Play();
audio.loop = true;
}
} else {
muzzleFlash.enabled = false;
enabled = false;
GameObject.Find(“MuzzleLight”).GetComponent(Light).enabled = false;
if (audio)
{
audio.loop = false;
}
}
}
}
10. This function is called by the PlayerWeapons.js script. It manages when the gun can
fire by comparing the time since the last shot was fired to the fireRate variable.
Unity scripts call the current amount of time since something happened with Time
.time and measure time as it passes with Time.deltaTime . Therefore, the nextFireTime
variable is the current Time.time minus the Time.deltaTime since the last shot was
fired. The while part of the if-while statement fires a shot if nextFireTime is less than
Time.time and if there are bullets left in the clip.
function Fire () {
if (bulletsLeft == 0)
return;
if (Time.time - fireRate > nextFireTime)
nextFireTime = Time.time - Time.deltaTime;
while(nextFireTime < Time.time && bulletsLeft > 0) {
FireOneShot();
nextFireTime += fireRate;
}
}
The next function plays the animation on HandgunRig called “shoot” and plays that
animation at double speed with GameObject.Find(“HandgunRig”).animation[“Fire”]
.speed = 2 . This is useful when you find that an animation is either too fast or slow,
because you do not need to return to your animation program to change and re-export
your model. If you are not using the HandgunRig model from the online resources,
comment out these lines.
Search WWH ::




Custom Search