Skip to content

Instantly share code, notes, and snippets.

@tracend
Created March 29, 2011 23:57
Show Gist options
  • Save tracend/893604 to your computer and use it in GitHub Desktop.
Save tracend/893604 to your computer and use it in GitHub Desktop.
Unity3D: A shooting routine - Source: http://wiki.dreamsteep.com/Unity3d
//My First Unity tool , it will shooot stuff , Keith Legg Jan 24
//to use drag this script to the main camera (or a vehicle's transform node ) , then drag a prefab to the bullet channel on the right
//be sure to adjust force/rate of fire so bullets dont hit each other when instantiated (example: higher force = slower the rate)
//space bar will fire gun on positive Z axis
//for a good default try 1000 force , 3 shots , and .1 second delay
var bullet: Transform;
var shootForce: float ;
var fullauto: boolean = true;
var number_shots: int ;
var delay_shots: float ;
//"machine gun"
function shootfullauto () {
for (a=0;a<=number_shots;a++){
var instancedbullet =Instantiate( bullet ,transform.position,Quaternion.identity);
instancedbullet.rigidbody.AddForce(transform.forward * shootForce);
yield WaitForSeconds(delay_shots);
}
}
function Update () {
var timer : float = 0.0;
if (!fullauto){
if(Input.GetButtonDown("Jump") )
{
var instancebullet =Instantiate( bullet ,transform.position,Quaternion.identity);
instancebullet.rigidbody.AddForce(transform.forward * shootForce);
}
}
if (fullauto){
if(Input.GetButtonDown("Jump") )
{
shootfullauto();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment