Slightly different solution

Before seeing how it was done in the lecture, I did it this way…:

Collision issues…
I created an EnemyProjectile script to attach to the enemy projectiles - and then when checking what had collided, the player was looking only for EnemyProjectile, and enemies were looking only for the player’s Projectile. This meant that no offsets were required for the projectile start-points, and I had no issues with enemy projectiles hitting other enemies.
But it DID mean duplicating code in both… I’m wondering if there is something that could be done to resolve that with inheritance? but I’m not really sure.

Random shots…
I selected a randomness for each enemy at the start - and simply used InvokeRepeating.:

void Start () {

    float randomFireRate = fireRate * (UnityEngine.Random.value + 0.5f) ;
    InvokeRepeating("Fire", randomFireRate, randomFireRate);
}

So basically, we are taking a random number between 0 and 1. Adding 0.5 to make it a random number between 0.5 and 1.5 - and multiplying that by the provided fire-rate. I also added the same delay before they started firing too so that all enemies didn’t start firing at exactly the same time - and the player has a bit of a chance to move at the beginning!

I kind of like this way and hope it won’t make things difficult in terms of how the future lectures go.

2 Likes

Actually I made another thing to get random shot by enemy and I want to share them too:

bool fireUpArmy = false;

void Start()
{
	if (!fireUpArmy) 
	{
		InvokeRepeating ("shotToPlayer", Random.Range(1f,3f), Random.Range(2f*fireFreq,6f*fireFreq));
		fireUpArmy = true;
	}
}

Is It Easier Than Lecture?

  • Yes
  • No

0 voters

I also used InvokeRepeating in Start() function to have enemies fire with a randomized firing rate. Seems to work just fine, looks similar to the video results and seems to be easier to implement than the probability stuff done in the video. Plus it just meshes with the way the player was done.

My guess is they just wanted to introduce the concept of probability - could come in useful in another game.

I’m not positive since I’m new to Unity, but I wonder which method is easier on the system? I did a quick Google and found this: https://gamedev.stackexchange.com/questions/120553/more-efficient-using-invokerepeating-or-update

Which seems to indicate neither is good - we should be using coroutines.

Also, @Brian_Robson I know you posted this ages ago… but perhaps still useful to others. To answer your question you don’t need a separate Projectile script for the enemy. What I did was add a tag called “Enemy” to enemy objects. Then in projectile class I added a function which I use in PlayerController and EnemyController (or whatever you call them) to determine how to handle projectile collisions.

public bool IsEnemyProjectile()
{
if (gameObject.tag == “Enemy”)
return true;
else
return false;
}