- Home /
How can I make robot fire a machine gun?
Using the robot from the FPSTutorial, is there a way I can make it fire a machine gun instead of fireing a rocket launcher? Also if I have a model with a @walk @idle @shoot @run models with it can I use the script for the robot on my model?
Answer by Ben 9 · Jun 21, 2010 at 01:16 AM
yes, and here's the code for shooting the machine
/* create and attach a firepoint to this script, also create a bullet(Has to be a rigidbody) as a prefab and drag the prefab to the 'Projectile' variable. bulletcount is how many bullets there is in one magazine before it reloads and power is the power you shoot the bullet */ var firepoint: Transform; var projectile: Rigidbody; var bulletcount: int = 80; var power : int = 1000; var ReloadDelayTime : float = 20; //////////////////////////////////////////////////////////// private var currentreloaddelaytime : float; private var overallbulletcount: int; private var currentbulletcount: int; private var reloading : boolean = false; //////////////////////////////////////////////////////////// function Start() { //////////////////////////////////////////////////////////// currentbulletcount = bulletcount; }
// Instantiate a prefab with an attached Missile script function Update () { //////////////////////////////////////////////////////////// if (currentbulletcount >= 1 && delaying == false) { if(Input.GetButton("Fire2")) { var bullit = Instantiate(projectile, firepoint.transform.position, firepoint.transform.rotation); currentbulletcount -= 1; overallbulletcount += 1; bullit.rigidbody.AddForce(firepoint.transform.forward * power); } } //////////////////////////////////////////////////////////// if (currentreloaddelaytime >= ReloadDelayTime) { currentreloaddelaytime = 0; currentbulletcount = bulletcount; reloading = false; } //////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////// if(currentshootdelaytime == shootDelayTime) { delaying = false; currentshootdelaytime = 0; } } ////////////////////////////////////////////////////////////
function OnGUI() { //////////////////////////////////////////////////////////// if (reloading == false){ GUI.Label(Rect(30,40,160,80), "Machinegun Ammo = " +currentbulletcount); } else{ GUI.Label(Rect(30,40,160,80), "reloading"); } //////////////////////////////////////////////////////////// GUI.Label(Rect(30,80,160,80), "You shot " + overallbulletcount + " bullets"); } //////////////////////////////////////////////////////////// function LateUpdate(){ //////////////////////////////////////////////////////////// if (currentbulletcount <= 0) { currentreloaddelaytime += Time.deltaTime; reloading = true; } ////////////////////////////////////////////////////////////
}