Skip to content

Instantly share code, notes, and snippets.

@bjpeterdelacruz
Last active December 15, 2016 22:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bjpeterdelacruz/c77d88e0dea669b6a971ed0fd44510c1 to your computer and use it in GitHub Desktop.
Save bjpeterdelacruz/c77d88e0dea669b6a971ed0fd44510c1 to your computer and use it in GitHub Desktop.
How to Spawn Arrows
IEnumerator SpawnArrows()
{
yield return new WaitForSeconds(startWait);
while (true)
{
for (int i = 0; i < arrowCount; i++) {
if (!isStunned) {
// is archer facing right?
bool facingRight = ((int) _transform.rotation.eulerAngles.y) == 0;
// calculate origin of arrow (0.5 pixels to left or right of archer)
float xPosition = _transform.position.x + (facingRight ? 0.5f : -0.5f);
Vector3 spawnPosition = new Vector2(xPosition, _transform.position.y - 0.2f);
// create arrow from prefab
Transform a = (Transform) Instantiate(arrow, spawnPosition, Quaternion.identity);
// set direction of arrow (right or left)
a.localScale *= (facingRight) ? -1 : 1;
Vector2 velocity = new Vector2(facingRight ? 5 : -5, 0);
// set speed of arrow
a.GetComponent<Rigidbody2D>().velocity = velocity;
// destroy arrow after some time has passed
Destroy(a.gameObject, destroyAfter);
yield return new WaitForSeconds(spawnWait);
}
}
yield return new WaitForSeconds(reloadWait);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment