Alternative Method to Arrow Lifespan:

I noticed the stack up of the arrows, while I was completing the continuous fire routine, and before Rick stated his challenge to destroy the arrows with a Shredder (which I went ahead and added anyway) I coded a seperate method, using a Parameter called arrowLife. It detroys the arrow after the duration that arrowLife is set to. I created a range slide for testing purposes and determined that the time it took an arrow to fly from the bottom of the screen is 2.5, so initialized the viable there and everything worked great!

my float paremeter

[Range(.1f, 10)] [SerializeField] float arrowLife = 2.5f;

this is what is called by the CoRoutine:

private IEnumerator FireContinuously()
{
while (true)
{
GameObject arrow = Instantiate(
arrowPrefab,
transform.position,
Quaternion.identity) as GameObject;
arrow.GetComponent().velocity = new Vector2(0, projectileSpeed);
Destroy(arrow, arrowLife);
yield return new WaitForSeconds(projectileFiringPeriod);
}
}

While I am sure they will be useful for other things, this eliminates the need to use a collider, or write a second script.

1 Like

Nice bit of testing. Extends well from the process we used in the Block Breaker segment, while adding in the useful aspects shown thus far in this part of the course. Also if you’re using a Unity version from 2019 onward, you don’t need to declare “as Object” at the end of instatiation - it’s apparently redundant.

I did a similar thing when testing out the WorldToViewPort() function, and found exact (relative) values to set the player ships position to. Works better than that padding Rick gave us.

Privacy & Terms