I’m trying to modify the Angry Bots autofire script.
So, you want to be able to either:
a.) Tap the fire button and shoot only one shot
b.) Hold down the fire button and shoot repeatedly at a fixed rate
The original script just played and stopped the audioclip “onStartFire” and OnStopFire" signals. As it stands now, it fires one shot cleanly using the “PlayClipAtPoint” command but doesn’t rapidfire. I’m sure it’s because I don’t understand the Vector3 command.
@script RequireComponent (PerFrameRaycast)
var bulletPrefab : GameObject;
var spawnPoint : Transform;
var frequency : float = 10;
var coneAngle : float = 1.5;
var firing : boolean = false;
var damagePerSecond : float = 20.0;
var forcePerSecond : float = 20.0;
var hitSoundVolume : float = 0.5;
var gunPlay : AudioClip;
var gunStop : AudioSource;
var muzzleFlashFront : GameObject;
private var lastFireTime : float = -1;
private var raycast : PerFrameRaycast;
function Awake () {
muzzleFlashFront.SetActive (false);
raycast = GetComponent.<PerFrameRaycast> ();
if (spawnPoint == null)
spawnPoint = transform;
}
function Update () {
if (firing) {
if (Time.time > lastFireTime + 1 / frequency) {
// Spawn visual bullet
var coneRandomRotation = Quaternion.Euler (Random.Range (-coneAngle, coneAngle), Random.Range (-coneAngle, coneAngle), 0);
var go : GameObject = Spawner.Spawn (bulletPrefab, spawnPoint.position, spawnPoint.rotation * coneRandomRotation) as GameObject;
var bullet : SimpleBullet = go.GetComponent.<SimpleBullet> ();
lastFireTime = Time.time;
// Find the object hit by the raycast
var hitInfo : RaycastHit = raycast.GetHitInfo ();
if (hitInfo.transform) {
// Get the health component of the target if any
var targetHealth : Health = hitInfo.transform.GetComponent.<Health> ();
if (targetHealth) {
// Apply damage
targetHealth.OnDamage (damagePerSecond / frequency, -spawnPoint.forward);
}
// Get the rigidbody if any
if (hitInfo.rigidbody) {
// Apply force to the target object at the position of the hit point
var force : Vector3 = transform.forward * (forcePerSecond / frequency);
hitInfo.rigidbody.AddForceAtPosition (force, hitInfo.point, ForceMode.Impulse);
}
// Ricochet sound
var sound : AudioClip = MaterialImpactManager.GetBulletHitSound (hitInfo.collider.sharedMaterial);
AudioSource.PlayClipAtPoint (sound, hitInfo.point, hitSoundVolume);
bullet.dist = hitInfo.distance;
}
else {
bullet.dist = 1000;
}
}
}
}
function OnStartFire () {
if (Time.timeScale == 0)
return;
firing = true;
muzzleFlashFront.SetActive (true);
//if (audio && gunPlay.isPlaying){
// yield WaitForSeconds (gunPlay.clip.length);
// gunPlay.Play ();
//}
//else {
audio.PlayClipAtPoint (gunPlay,Vector3 (5, 1, 2));
//}
}
function OnStopFire () {
firing = false;
muzzleFlashFront.SetActive (false);
if (audio){
gunStop.Play ();
}
//else {
// gunStop.Play ();
//}
}
Just a quick tip: your Question suffers from information overload. Try to be more concise so it doesn't take people so long (or scare them off entirely) to figure out what your question is!
– Huacanachayou seem rather well informed for a newbie :P here is a thought you could add the audio source to the bullets them selves. then give them a 1 shot bullet sound. which will eliminate the cut out effect. this will also mean that the sound will play as the bullets are ejected. you could also add a Pitch randomization to the sources so it doesn't sound so uniform. the only down side is that the sound may "diminish" very quickly due to the bullet being ejecting so fast that it gains too much distance too quickly. (bullets can travel up to 6000ft per second!)
– Fornoreason1000Could something in this script I found be made to work? I'm not a good programmer.
– rootedvar fireRate = 0.5; private var nextFire = 0.0; private var i = 1; function Update() { if(Input.GetButtonDown("Fire1")) { Debug.Log("Fire1nce: " + i); nextFire = Time.time + fireRate; i++; } if(Input.GetButton("Fire1")) && (Time.time > nextFire)) { Debug.Log("Fire2ice: " + i); nextFire = Time.time + fireRate; i++; } }