- Home /
Problem with Shotgun
Hey guys! I took the script I have for multiple weapons and decided to make a shotgun, but I'm having some trouble getting it to shoot multiple times at once!
Here's the relevant code:
function FireShotgun() //this just does the stuff that needs to happen once
{
playerCam.SendMessage ("Fire");
FireSound();
currentClip -= 1;
if (currentWeaponSpread < maxSpread) { currentWeaponSpread += spreadIncrease; }
FlashMuzzle();
ShellEject();
BarrelSmoke();
Recoil(); // this calls the recoil to calculate...
camContain.GetComponent(CameraRecoil).Recoil();
FireDelay();
nextFire = false;
ShotgunBullets(); //this starts the raycast loop
}
private var amount = 0;
function ShotgunBullets()
{
if (amount < burstAmount) {
randAngleRay = Random.Range(0, 359);
randRadiusRay = Random.Range(0, currentWeaponSpread);
randRayX = ((Mathf.Sin(randAngleRay)) * randRadiusRay * .01);
randRayY = ((Mathf.Cos(randAngleRay)) * randRadiusRay * .01);
var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*(0.5 + randRayX), Screen.height*(0.5 + randRayY), 0));
var hit : RaycastHit;
//isShooting = true;
// insert spread stuff here
if (Physics.Raycast (ray, hit, 500, layerMask))
{
Debug.Log("Shot" + amount);
var bulletHole = Instantiate(bulletDecal, hit.point, Quaternion.LookRotation(hit.normal));
var bulletDust = Instantiate(hitDust, hit.point, Quaternion.LookRotation(hit.normal));
var bulletDebris = Instantiate(hitDebris, hit.point, Quaternion.LookRotation(hit.normal));
Sparking = Random.Range(0, 10);
if (sparkFactor >= Sparking)
{
var bulletSpark = Instantiate(hitSpark, hit.point, Quaternion.LookRotation(hit.normal));
}
hit.transform.SendMessage("ApplyDamage", Damage, SendMessageOptions.DontRequireReceiver);
Debug.Log("hitsomethin");
Tracing = Random.Range(0,10);
if (tracerChanceOutOfTen >= Tracing)
{
var instantiatedProjectile : Rigidbody = Instantiate(Tracer, muzzleTip.transform.position, muzzleTip.transform.rotation );
instantiatedProjectile.transform.LookAt(hit.point);
instantiatedProjectile.rigidbody.AddForce(instantiatedProjectile.transform.forward * tracerSpeed);
Physics.IgnoreCollision( instantiatedProjectile.collider, collider );
}
}
Debug.Log("Exited");
amount++;
ShotgunBullets(); //this loops it until it's done enough to equal the amount of bullets that get shot
}
else { amount = 0;}
}
I have the amount set to 10, but all I get in the debug.log is a single "Shot0" and that's it!
Help!
Comment
Answer by Hullu · Jul 28, 2014 at 08:53 AM
Use "for" loop:
Replace this:
if (amount < burstAmount) {
//Shoot
amount++;
ShotgunBullets();
}
With:
for(var i = 0; i < burstAmount; i++)
{
//Shoot
}
edited
Your answer
Follow this Question
Related Questions
Shotgun script 2 Answers
Shotgun raycast 1 Answer
Making a shotgun 3 Answers
Raycast Shotgun 1 Answer
Shotgun Spread Issue 1 Answer