Last active
December 15, 2016 22:00
-
-
Save bjpeterdelacruz/c77d88e0dea669b6a971ed0fd44510c1 to your computer and use it in GitHub Desktop.
How to Spawn Arrows
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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