(SOLVED) Make Enemy Change Direction after Colliding with a Draggable/Movable Object

What is the expected result

I want to make it so that when an enemy (RunnerAI) is near or collides with another object (ClickableObject), it’ll change and move in the other direction. Maybe the enemy would be pushed back a bit when colliding or close to the object.

By extension, when this other object is moved, the enemy will continue running now that its path is clear.

What is the actual result

When I boot up the game, the enemies will run into the other object, but won’t change direction.

I’ve looked up tutorials on youtube and threads here, but I haven’t found anyone covering what I’m trying to do.

Related screenshots



Hi Jules-Mado, I’m probably not going to be the one to solve your question. However, I think there’s a problem with your boolean variable ‘Facing’. You change it to true or false based on RunnerAI either being in collision with GoLeft or GoRight.

But then when RunnerAI is within 100 pixels of ClickableObject, you toggle the boolean variable ‘Facing’. So the toggled state depends on which of GoLeft or GoRight it recently collided with? Is this what you want.

Would you be better off having two different boolean variables for the GoLeftRight stuff and Clickable Object? And explicitly change the Facing variable to true rather than toggling it.

1 Like

But then when RunnerAI is within 100 pixels of ClickableObject, you toggle the boolean variable ‘Facing’. So the toggled state depends on which of GoLeft or GoRight it recently collided with? Is this what you want.

I guess, yeah. When the enemy is about to collide with the clickable object, the programming should reverse the enemy’s movement. Booleans only have two states, so I thought I can tell the program if it’s not on one state, then it’s the other.

I’m open to the idea of adding another boolean, but how could I add that without it’s programming coming into conflict with the first boolean’s programming?

Also, I used the enemy’s animations to see what could be happening and I found something.
In the screenshot below, I have it set up so that the enemy switches animations depending on the boolean’s state. When it collides with the clickable object, the animation doesn’t change. This means that it’s not even acknowledging the condition with the object and that’s why the boolean isn’t changing. The enemy stops at the object because it has the Platform behaviour. I have it set to solid platform, not because of my programming.

I guess the question now is how do I get the program to acknowledge the object?

(internet is awful today so i can’t upload screenshot until later)

I understand it now.

But should a collision even happen, Because you toggle the variable when the enemy is within 100 pixels, not when it’s colliding. Did you change something?

1 Like

Have you tried using “trigger once” command on the GoLeft and GoRight events? Otherwise when the collision happens it keeps triggering from that point onward, clashing with future collisions. And like Bubble said, toggling the Facing variable within 100 pixels but also using the same boolean variable it seems likely to cause clashes. Might be better to have different variables and to make sure that they are only triggered once, each time a collision or pixel distance happens.

You could also alternate between the collision and the 100 pixels by creating a number variable and selecting it’s value randomly depending on the situation. Something like this…

Condition
If within 100 pixels
Action
set Variable(GoLeft) to Random(1,2)

Condition
If Variable(GoLeft) = 1
Action
do nothing

Condition
If Variable(GoLeft) = 2
Action
change direction now

So once it’s within 100 pixels it will either pick option 1 or 2. If it picks option 1, it doesnt change the direction. In that case you have another variable which will only trigger if the enemy collides with the object.
If it picks option 2, it will immediately change direction.
Using Random it makes the enemy movement less predictable.

1 Like

Hi and thanks for waiting. There was a Cold Snap this weekend so the internet connection was weaker thanks to the heavy winds outside and I couldn’t upload anything here or on other sites.
Here’s what I changed as of Friday. It doesn’t work either, but I’ll fix it up.
I saw that you and someone else has made suggestions, so I’m gonna try those today and update here tonight.

1 Like

So, I experimented a bit more and made a very interesting discovery:

First off, the Clickable Object/ green crate has the Draggable Behaviour.

When the enemy touches the crate, nothing happens right?

But when I click and drag the crate over the enemy, THEN it changes animation and direction as I wanted. I think the problem lies in the collisions!

It’s strange, though. In the “Enemy is in Collision with Crate” condition, I made sure to click No on the bit about Ignoring objects that are touching edges but not over lapping.

I tried clicking Yes on that to see what it would do, but that had no effect either.

So, it seems like the programming is working but for some reason, only works when the the enemy is inside the crate’s collision mask, not touching it.

SOLVED:


This is the how I got an extra object to change an enemy’s direction - BUT it only works if the object either doesn’t have the Platform Behaviour installed or the Platform Behaviour is installed but it’s not set as a solid platform.

This can work with either the “two objects are close to each other” condition that I have set up in the screenshot or the “collision between two objects” condition.

Thank you Bubble and Gorguruga for your patience and suggestions. I was able to get to the root of the problem thanks to talking to you two and experimenting with your suggestions.

2 Likes