Enemy movement issue (SOLVED)

I’m making a top down game, and I’m currently making the enemies,

the direction the enemy goes is based on the value of the current variable.

When the enemy touches a wall or a gap, it’s supposed to change to a different variable that has it go the opposite direction.

But when it touches a wall, it just stays there.

Here are my events for the wall/gap collision:

It’s also worth noting that I said it so it ignores collisions that are touching on the edge.

How do I fix this? :thinking:

Does it move the enemy away from the wall/gap?

It’s supposed to. The separation events are meant to separate the enemies and the walls/gaps.

But is it happening? That will help determine if the problem is in the Conditions or the Actions.

I think the trigger once will be causing the problem. It’s a long story, so in a few word, it doesn’t behave per object, rather once for all the objects.

I think the following screen snip should be all you need to get your enemies to turn around. This will be a more efficient, particularly if you have a large number of enemies and/or run the game on slow devices :

Edit - put the Wall and Gaps objects into an object group named enemy_turners

1 Like

I did what you showed me, and it worked for going down, but what about the other directions?

What are the value of direction for each of up, down, left and right?

down = 0

up = 1

left = 2

right = 3

Good, that makes it easy to amend. Change it to the following, which should work for all directions :

How do I do the += symbol?

It’s just the add option. I’ve probably got a different display setting to you.

It didn’t seem to work. I did exactly what you showed me.

Yeah, I wasn’t 100% on it. Can you move the OR conditions as part of the repeat block (so it’s in that first event and not as a subevent)?

Also, as a separate note:

Never use multiple “For each” events if you can avoid it. Because you’re having them occur in different loops.

You should have a parent “for each” with subevents that have your branches. Otherwise you could run into issues where each loop applies to multiple instances when you don’t want it.

(The bottom for each is probably fine since it’s related to a separate or statement)

1 Like

It didn’t work…

This is a lot harder than I thought.

If I can’t figure it out, I’ll just use direction changing objects.

I was just trying to find a way to do It without having to make direction objects for the enemies…

Have you tried it with just one enemy and one wall object? So create a very simple scene, with the bare minimum of events.

That’s what scene I was testing it on.

Can you write the value of the enemy’s direction when it collides with the wall to console, and again straight after when it’s supposed to be changed?

And does it still only work for a downward moving enemy?

If I remember correctly there is actually a tutorial for this type of events. In platformer tutorial you can change enemies direction via collision. I’m not sure but it helped so it may help you too :slight_smile: Here you go: Platformer Tutorial, Part 6: Add Enemies to the Game [GDevelop wiki]