New enemy won't follow new path

Hi guys, when I try to add a new enemy with a different laser and give it a new path, it only spawns 1 enemy outside the game screen, it won’t follow the new path I made for it and it actually doens’t even spawn the 1 enemy on the first waypoint :thinking: What could I have done wrong. I tried changing the path of my last enemy-wave to one of the other paths, and that works fine, it spawns the enemies and they shoot off different lasers. So something is wrong with my new path, but I cant seem to figure out what…

I have attached a screenshot here, and as you see in the screen window, the new enemy is outside the game window, shooting his lasers. I can also send my project file if needed.

Best Nicklas

Hi,

Did you create a new wave and add both this enemy and the path to the wave? Did you then add that wave to the EnemySpawner?

Hi Rob, yeah, did all that, but it still doesnt work, If I change my new wave to another path, then it works, so something is up with my new path, but it’s actually duplicated from one of the other paths and just moves the waypoints around alittle and saved it as a new prefab. No workie :man_shrugging:

Best Nicklas

I’d be happy to take a look if you want to zip up the project files and share them with me?

The forum will allow uploads of up to 10MB, if your project files (zipped) are larger than that you would need to use a service such as Google Drive or Dropbox, and then share the URL.

Thx Rob, that would be great. I’ve created a wetransfer link as the zipped project is a little big

When you open, the wave’s all work fine, but if you try to edit Wave C2 in the waves folder from Path (0) to Path (2) you will notice the enemy spawns outside the game window. Can’t figure out why my path (2) wont work :thinking:

Thx a bunch

N

Hi Nicklas,

This was a fun one to diagnose but we got there :slight_smile:

Have a quick look at this video;

At the beginning I turn off looping just so that we get the 6 enemy ships, a little easy to work with. We believe the enemy ships are not moving and just sit there, however, when I grab one of the enemy ships and move it directly with the transform tool you can see it works it’s way back to the original spot.

If we then look at the code that handles the moving;

private void Move()
{
	if (waypointIndex <= waypoints.Count - 1)
	{
		Vector3 targetPosition = waypoints[waypointIndex].position;
		float movementThisFrame = waveConfig.GetMoveSpeed() * Time.deltaTime;

		transform.position = Vector2.MoveTowards(transform.position, targetPosition, movementThisFrame);

		if (transform.position == targetPosition)
		{
			waypointIndex++;
		}
	}
	else
	{
		Destroy(gameObject);
	}
}

We know at this point that the enemy ship are moving, but they are only moving towards that first waypoint, the waypoint never increments. If we look at the specific code for that;

if (transform.position == targetPosition)
{
	waypointIndex++;
}

This states that only when the enemy ship reaches its target should the waypoint increment, in this scenario the enemy ship cannot be reaching its target position. Looking at the scene only (2D <- big hint there), it looks as if the enemy ships are at their target. Let’s look at what that target position has been set to;

image

image

Looks ok doesn’t it, absolutely no obvious reason why the enemy ship couldn’t get to that position.

But remember, the Waypoint GameObject in the prefab is a child of the Path (2) GameObject, lets take a look at that;

image

image

Remember, this is a 2D game… :wink:

So, the parent Path (2) GameObject has a Z axis value of -86.342. So what is happening is that your enemy ships are moving, on the X/Y axis, which are the only ones they are being allowed to move on, but cannot move on the Z axis to ever reach their target.

If you compare Path (2) to your other paths you’ll note that they all have a Z axis value of zero. Lets make that change :slight_smile:

One thing I would suggest is setting up a clearly defined rule for the creation of your paths. For example, if you reset the transform of the main parent GameObject and then only adjusted the X, Y, Z values for the child waypoints you would avoid this issue, as they would not inherit anything from their parent. Another approach would be to set the transform position of the parent GameObject to be the same as the first waypoint, by doing so when ever you select one you would be taken, in Scene view, to the first waypoint location (unless you move it). Personally I’d favour the former, just reset the transforms and make that a good habit to work to.

Hope this helps :slight_smile:

Ahhhhhhhhh, of course :smiley: That makes sense now, funny that the Path(2) would change it’s Z axis number, since I duplicated it from the other Path :thinking: Oh well, thank you so much again Rob, great help :beers:

Best N

1 Like

You’re very welcome.

It could have just been a bit of rapid mouse action, perhaps you caught the label for the Z axis field and it just scrolled through some numbers, it happens. :slight_smile:

Felt kinda sorry for your enemy ships, they were trying so desperately hard to follow their orders but weren’t able to move from zero on the Z… hehe :space_invader:

Privacy & Terms