(Solved) Npc collision detection

So i have made random npc movement but the problem is the npc just collides with something and then just keeps moving while getting stuck in the collision instead of avoiding it
here’s the event i am using for the npc to avoid the stuff

The separate objects action just separates the objects. It doesn’t change any force that’s being applied to them. You would need to change the direction of the force otherwise they’ll just keep running into a wall or whatever. How are you moving the NPC?

An extension like bounce is good for a pong or bouncing ball effect but it depends on how the force is applied and the desired outcome.

i am using this event

Is it working completely off a timer? You might need to add an or if timer or collision between objects. Otherwise it will keep walking into the whatever until the time elapses.

yes only timer
i have added collision but it just keeps ramming in the collision
so i don’t know what to do

How did you incorporate the collision? Can you post your current events. Something like NPC in collision or timer elaped then change direction, reset timer …

Does Alice and the NPC object move? I’m unsure about the Alice events. Is Alice an NPC?

Sorry i confused you
Alice is an npc and the NPC is group of different npcs
the ones i have posted above are the events i am using

I’ve never had much luck with avoidance techniques like that. IDK if you’re familiar with the pathfinder behavior. It’s really good for navigating.

It uses 3 objects; the character, the rock and a shared sprite to test the random location. The characters have pathfinder behavior. The rocks have the pathfinder obstacle behavior. The characters are in a group name AliceGroup and the rock is in a group named ObstacleGroup

The nice thing about groups is that you can add or remove objects without modifying any of the events. This works with different objects or instances of the same object.

The method is fairly simple. Add a tester object at the start. The characters are the same size, so it only needs 1 tester object. When AliceGroup isn’t moving place the tester object at a random location. In case the random location is in the middle of a rock, I added the separate objects. Then it moves the character to the position. You can use has reached destination but checking the speed takes care of several things. It starts the character at the beginning of the scene, it starts it when the destination is reached and it picks a new location should the character get hung-up on the edge of a sprite.

Now, pathfinding works by planning a route when you set a destination. So, it doesn’t know if objects get moved after it sets it route. I added a collision test in case something blocks its path. A timer could probably be added so the character waits a little first but this looks good to me. They don’t target the X, it’s just there for testing, I left it visible to make sure it worked and to make the method easier to understand.

try it: I made the rocks draggable so they can be placed in front of the character to show how the character will pick a new location if blocked.

project: (click the green [code] button and select download zip. Unzip and open the json file with Gdevelop)

So i did as you said but i am having a problem

the npc is not playing any animation and the npc just is not in screen area i don’t know how to explain
(when the player reaches the point where npc’s are suppose they have wandered too far i think)

Using the isometric game template for inspiration, I created another example. If your project moves the camera then the random x,y needs to be based on the characters x,y. I added events to set the start position of the objects and then pick a random location based on a range from the character’s starting position. I set the distance to a small value for demonstration. I also added events for animation and to enable/disable the movement based on the distance from the player using the MovementDistance object variable with a little extra space. The movementDistance is based on a circle from the starting position and not a square. So, if it’s 100 then it’s 100 in each direction.

try me: I added a happy face as the player. it uses the default cursor controls to move

project:

2 Likes

So i tried and it is working really nicely
but i wanted to ask you a question

if my npc group contains npc’s with different names then do i have to do the play/pause animation for each of them?

I don’t think so as long as the animation names are the same and they’re in a group. The group name gets treated like an object name. You might need a for each object it’s hard to tell how actions will behave. Sometimes they need for each object sometimes not. Try it as is with the different objects in a group.

Edit: I see my example used Alice2, if it’s a group then that would have to be the group name. My group only had Alice2. So, it worked.

Edit 2: I changed the object name to the group name, added a 2nd object to the group and it works fine. I update my github project but the only difference is fisherman to npcGroup. It was also already inside of a repeat for each object

Did that and it is working nicely thank you very much

1 Like