Skip to content

Instantly share code, notes, and snippets.

@mecha-moonboy
Last active October 31, 2018 19:15
Show Gist options
  • Save mecha-moonboy/4c749617ab4e343346619a460290cc24 to your computer and use it in GitHub Desktop.
Save mecha-moonboy/4c749617ab4e343346619a460290cc24 to your computer and use it in GitHub Desktop.
// The [makeBullets()] function was called earlier. It calls for an amount of bullets, an amount of damage that each bullet will deal, an origin point,
// another point to shoot towards, a float for a range, a float for the max spread, and a float for knockback force.
public void makeBullets ( int Quantity , int Damage, Vector2 Origin, Vector2 Direction, float Range, float Spread = 0f, float KnockBack = 0.0f){
// This entire for loop repeats for each bullet being created.
for( int i = Quantity ; i > 0 ; i-- ){
// Gets the point where the ray will end. Uses normalized [Direction], adds a random value to it multiplied by the [Spread],
// which means if spread is zero, the bullets will fly perfectly straight.
Vector2 rayEnd = new Vector2( (Direction.normalized.x + (Random.value - 0.5f) * Spread) * (Range + rangeMod), (Direction.normalized.y + (Random.value - 0.5f) * Spread) * (Range + rangeMod));
// Creates and defines the bullet variable as a ray shooting from [Origin] towards [rayEnd] but only as far as [Range].
RaycastHit2D bullet;
bullet = Physics2D.Raycast ( Origin , rayEnd, Range + rangeMod, ~layerMask );
// Creates a [lineRend] variable and defines it as a newly created line renderer.
LineRenderer lineRend = ((GameObject)Instantiate(bulletLinePrefab)).GetComponent<LineRenderer>();
// Sets the first position of the line renderer to [Origin].
lineRend.SetPosition( 0 , gun.transform.position);
// Checks if the raycast hit something AND the thing it hit is within range.
if ( bullet.collider != null && bullet.distance <= Range + rangeMod){
// Checks if the the thing the ray hit has a rigidbody.
if (bullet.collider.attachedRigidbody != null){
// Adds force to the rigidbody equal to [KnockBack].
bullet.collider.attachedRigidbody.AddForce(new Vector2 ( bullet.collider.transform.position.x - bullet.point.x , bullet.collider.transform.position.y - bullet.point.y ) * (KnockBack + knockbackMod), ForceMode2D.Impulse);
// If the thing it hit has the "Enemy" tag...
if (bullet.collider.tag == "Enemy"){
// Tell the enemy it hit to take damage equal to [Damage].
bullet.collider.SendMessage("Damage", (Damage + damageMod));
}
}
// Set the second position of the line renderer (the end of the line renderer) to the point where the ray hit a collider.
lineRend.SetPosition( 1 , bullet.point );
} else if (bullet.collider == null){ // If there wasn't a collider...
// Set the second position on the line renderer (the end of the line renderer) to the end of the ray, [rayEnd].
lineRend.SetPosition( 1 , rayEnd + new Vector2 ( transform.position.x , transform.position.y ) );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment