Why do my enemies destroy themselves?

Hi Rick

I have a problem: In this lecture we add a rigidbody2D and the DD script to the enemies. But now they destroy themselfs at the spawnpoint. After a few tests I figured out, that it is the rb2D. In static they spawn, but don’t do damage to me, kinematic and dynamic the are just a flicker on the screen.

In your game, everything is fine… what am I doing wrong?

Cya!

3 Likes

If they are capable of shooting projectiles, make sure they are not getting hit by their own projectiles. Maybe change it so the lasers spawn in front of the enemy’s collider for now.

If that works, we’ll talk about a better solution.

Most of them don’t exist long enough to shoot a projectile.
I would like to have a collison, so that the player can’t move through enemies.

Thanks for helping me.

Did you make the change? Are the projectiles spawning (or supposed to spawn) well clear of the enemy’s ship?

Now I made the change, even the projectiles were on a different layer.
I made a childobject on the enemyship and placed it infront of the collider.
Then I changed the instantiate to child.transform.position, serialized it and put the child in the field

What totally confused me was… the 1st, 3rd and 5th wave destroyed itself immediatly… buuut… the 2nd and 4th wave were totally fine…
I looked at them, but I can’t find anything special, all of them were duplicatetd from the 1st wave and I just changed the name and the waypoints…

Sorry for bothering you :blush:

But thank you very much

Ley’s check the path for the first wave.

Check the inspector for your EnemySpawner gameObject to see which wave is the first wave.
Look at the inspector for that wave’s scriptable object to see which path it has.
Check the path. Look at the prefab and the one in your heirarchy, if there is one.
Ensure that the path has a few points and they are not all together.

The Setup for the paths should be correct. I checked it.
But like I said, if I turn the rigidbody2D to static, the paths are working fine. And if I change it back to kinematic (for the Collision damage to the player) the enemies are just there for a milisecond before disapearing until the 2nd wave starts, which works fine… and so on…
Wave1 and 2 are the same enemy

Just to interject, it might be worth considering adding a Debug statement in your OnCollisionEnter2D method within the script(s) on the enemies. For example;

private void OnCollisionEnter2D(Collision2D collision)
{
    Debug.Log("Collision detected between " + gameObject.name + " and " + collision.gameObject.name);

    // ...
}

If there are collisions with other things, the above would give you the name of the thing in question, so, whilst you don’t see projectiles in the scene, if they are generated quickly, and then destroy themselves and the enemy ships, the above would give you an indication. It would also help if the enemies were colliding with each other and causing them to be destroyed - it also helps if none of the above is true, e.g. you can rule out a collision being the issue.

Just thought I’d share the above as it may help in your diagnostics. :slight_smile:

Thank you Rob
I will try this when I get home.

1 Like

No problem, let us know what you discover :slight_smile:

Okay, there was indeed a collision, well in this case it’s an OnTriggerEnter2D method.
I raised the spawn duration, but it’s still the same problem… enemies from wave 1, 3, 5 destroy each other.
Wave 2 and 4 are fine…:thinking:

Edit: Okay I figured it out… I checked the waypoints again and waypoint(0) was to far away from waypoint(1) :thinking::face_with_raised_eyebrow::dizzy_face: ???
I guess… Well I put the waypoints closer to the edge of the playspace and it worked… I really don’t know what the problem was. But it works now

Thanks guys! :hugs:

1 Like

Hi Dominik,

Glad to hear you managed to resolve the issue.

I should have mentioned about the OnTriggerEnter2D method also, sorry, I’ve not run through the updated course material so there may be some changes that I wasn’t aware of.

The solution seems quite odd, not sure why that should make a difference unless there was something else going on at the same time? Can you reproduce the issue easily? e.g. if you were to move waypoint(0) further away from waypoint(1) again, does the same issue occur?

Yes, well partially. If I move the waypoint(0) fromt the 2nd wave faaaaaar away it still works (this one worked before), but if I move the waypoint(0) fromt the 5th wave just 2 units away they start to destroy each other.
:tipping_hand_man:

Ah, I see.

So, there was a collision between enemy ships, and your solution (at the moment), is to simply move the waypoints further away to prevent the collision - is that right?

Assuming so, and as mentioned above I’ve not been through the update content yet so it may be mentioned in a lecture you’ve not got to, but you could just ignore the collisions where they are not relevant.

There are a couple of ways you could achieve this, one would be via code, in your OnTriggerEnter2D method, use an if statement to determine the type of thing that has made the collision and only do something if it’s something you care about. For example;

private void OnTriggerEnter2D(Collider2D other)
{
    Projectile missile = other.gameObject.GetComponent<Projectile>();

    if (missile)
    {
        // ...
    }
}

The above is a snippet from the original course content. It creates a local variable missile of type Projectile and then calls GetComponent on the GameObject other which was involved in the collision.

Next, a test is made to see if missile is anything but null, if it is, then it was a projectile that hit the ship and we can do whatever would be appropriate. If it wasn’t, nothing happens.

I appreciate that these types may not be used in the updated version of the course, Projectile for example, but hopefully the above outlines a way that you can be discerning when it comes to collisions and base a course of action upon it.

Another approach would be to use Layer based collisions. You would create layers for perhaps the enemies, and the player, then using the Collision Matrix you can simply tick/untick the collisions that you are interested in. If you were to untick the enemy/enemy option for example, you would not receive any messages when a GameObject in the enemy layer collided with another GameObject in the enemy layer - as such, your enemies wouldn’t destroy each other on contact. You could also add their lasers/missiles to the same layer which would then prevent them from shooting themselves also.

As mentioned above, this may already be covered in the updated content, so maybe check the names of the lectures before jumping ahead too far in case it’s something you’ll cover in the next lecture anyway :slight_smile:

Hope this helps :slight_smile:


See also;

1 Like

Well yes and no… there was a collision between ships, but i moved the points “closer” and it worked :smiley:
I watched the Wrap Up yesterday, so this course is over and I am about the implement a BossBattle and maybe some other stuff.
We did the Layers, I checked it and based upon that enemies shouldn’ be allowed to collide with each other, the same is with the projectiles…

Thanks for the help, I don’t know what the problem was, but it works now :smiley:
I will continue to implement some features and wait for the GlichGarden (I can’t wait!)
And when I know more about coding… maybe it clicks ;D

Thanks again Anthony and Rob for the help!

Cya guys

1 Like

Hmmm… Puzzling…

Oh well, enjoy the rest of the course and you are very welcome :slight_smile:

1 Like

facepalm of doom :man_facepalming:
I am really really sorry, that I wasted your time…
I, the genius that I am, spawned the enemies on the fricking projectileshredders…
and of course… the enemies got shredded…

can you please delete this post that I am no longer ashamed of myself… :sweat_smile::zipper_mouth_face::sleepy::rofl:

1 Like

Well, looking on the bright side, you have identified the issue yourself and know how to resolve it - well done.

Regarding your post, we can delete it, but all questions are really valuable. If another student has a similar problem and sees your post they may well learn something valuable from it. My preference would be to keep it unless you really want it gone.

It’s a friendly and supportive community, there is no shame :slight_smile:

2 Likes

Okay… we can leave it here i guess
But it’s a lot to read for a simple solution…

We have that covered… When your post is marked as a solution, a summary is placed in the first post of the topic :slight_smile:

1 Like

Privacy & Terms