[SOLVED/Help] Detecting attackers when/after they enter the screen

I am trying to tweak the shooter.cs so it will not put the defender into the firing animation before the attacker shows up on the field. Doing some digging, it seems like renderer.isVisible is the way to go but it’s not working as i expected. Any help or guidance would be appreciated.

Here is the snippit in question:

> bool IsAttackerAheadInLane() {
> 		// Exit if no attackers in lane
> 		if (myLaneSpawners.transform.childCount <= 0) {
> 			return false;
> 		}
> 		// If there are attackers, are they ahead of the defender?
> 		foreach (Transform attacker in myLaneSpawners.transform) {
> 			//attacker = attacker.transform.position.x >= 10;
> 			if ((attacker.transform.position.x > transform.position.x) && attacker.renderer.isVisible)  {
> 				return true;
> 			}
> 		}
> 		// Attacker in lane, but behind us
> 		return false;
> 	}

Thanks in advanced.

I just modified this line to not shoot unless they were at least at 10.5f.

This is what I did:

	// If there are attackers, are they ahead?
	foreach (Transform attacker in myLaneSpawner.transform) {
		if (attacker.transform.position.x > transform.position.x && attacker.transform.position.x < 10.5f) {
			return true;
		}
1 Like

Thanks Freddie_G. That solved it. I almost got to that solution :slight_smile: It’s working now. I just need to figure out what to do with my spawner next!

1 Like

You bet! The lessons in this course have been good, but the absolute best learning I’ve done has been modifying the projects to do things, just like you are doing.

You can check out my in progress Glitch Garden here: The Great Sugar War! (Glitch Garden) - Original Assets

I decided to make each spawner pull from two arrays - the first is the enemy to spawn, the second array is the time to spawn. This really helped me with level design. However the win condition then has to be all the arrays being empty instead of time running out.

Best of luck!

2 Likes

I checked yours out and it’s fun! I have alot of learn and grow (if only I was an artist). Would you mind sharing how you did the spawning?

Privacy & Terms