Collision with the same object

If i do an event where one object collides with other same object, how can i do to make one of the objects to stop and the other to continue walking? :confused:


You can try to use the “Pick a random object” condition / action in the event.

Thanks for answer! But what i need is that the objects cant move through another objects u know? If im walking and my character collides with an enemy, or the enemy collides with another enemy, that the objects can not pass, and then continue walking in another direction, like if they was obstacles but they can move :astonished:

The automatic way would be to use the action All objects>Position>Separate two objects which will give the appearance of objects/characters being solid.

If you want them to turn around and walk back the other way when they collide, then it’s a bit more complicated as you’ll have to keep track of which direction they were travelling in already and apply a force in the opposite direction (or reverse the sign of the velocity). If it’s an overhead view game then picking a random angle might be better.

Thanks for the answer! I have one last question, in my game when i shoot, if two enemys are too near eachother i kill both… i think its couse the bullet collides with all of them couse they are together, how can i do to kill only one if the bullet collide with multiple targets?? :frowning:

Delete the buller exactly at “first” collision. Not sure about the problem you’re facing, but deleting the bullet in first action after collision condition should work.

Also make sure you are using the For each object event:

By using this event you are going through every single instance of an object and trigger the events individually on all of them one by one, it helps to make sure actions are triggered only on the ones that met the conditions. In this case, collision with the bullet. So the event should looks something like this:

For each object Enemy, repeat: enemy is collision with bullet : Delete object enemy

It may also helps with your original problem to check collision between the instances of the same object:

For each object Enemy, repeat: enemy is collision with enemy : change direction of enemy

ddabrahim, the “for each” in your first “for each” example is useless.

I’m having similar troubles with instances all the time, for each is my best friend to solve them :laughing:
But I guess you are right, ideally it should work without for each… :stuck_out_tongue:

In fact, if you don’t need to act on each instances independently, the “for each” is useless. However, when you act on a list of instances using expressions (numerical or texts) related to them, then you’ll need the “for each” to reprocess the result of the expression for each instance.

THANKS SO MUCH FOR THE HELP TO EVERYONE!!! I have one last problem, and sorry for being so handless :cry: , i did the All objects>Position>Separate two objects, to make objects solid, but the problem is, i did an event that if the enemy is alive and is not colliding with a carrot ( the enemys are mouses that eat carrots xddd… ) they will walk to the right, and i placed them one behind the other, so when i put Event: “when they collide with another enemy” do Action:“All objects>Position>Separate two objects”, but the problem is they still mooving and push the other enemy, i dont want them to push the other enemy i want them to stop and wait till the first mouse mooves, to move the second, then the third… etc… And if i put they to stop when they collide each other they will stop for ever, and if i put a timer, the mouses go to diferent random places LOL… so i dont know how to do that.

** The idea of the game is that some mouses come from the left to eat the carrots that i have to defend with my character that shoot with a shootgun… I think this can be the game that make me rich, and then i will share the dolars with this community of course :unamused: xD.


It doesn’t seems to be a complicated game so if you have a hard time making it, you may have made some poor design choices.
If I understand what you trying to do, you just simply have a bunch of carrots on the screen and you want some mouse come for the carrots that you need to kill, if you don’t kill, the mouse get the carrot, if you run out of the carrot you loose. And probably as the time goes you also want the mouse coming for the carrot more quickly and more often to make it challenging after a while.

If this is what you want, personally I would not bothered if the mouses are colliding with each other unless it is important and to keep moving the mouses back to the beginning to go an other round.

What I would do is, I would select 2-3 places where the mouses would coming from and I would keep creating a mouse always in a random position. I would also setup a timer to control the speed, how often the mouses going to be created. At the beginning let say I would create a mouse every 5 seconds and as the time pass, I would decrease the time between two mouse and the mouses would be created faster and faster. Once a mouse have created, it would select a random carrot to move toward to and once a mouse get to the carrot, it is colliding with the carrot, the carrot would be deleted and the mouse as well since the next one is going to be created anyway or it could pick up the carrot and run away, in that case the carrot would be still deleted but the mouse would change it animation for carrying a carrot maybe. If you can still shoot the mouse while carrying the carrot, it would drop the carrot (create a new carrot in position of mouse) and delete the mouse. If no more carrot left on the scene, Game Over.

This is how I would design this game, but in case this is not exactly what you want, feel free to share more details about your game. But the fact is, it is difficult to tell what is the problem with you game exactly, we should see the events you have problem with, so if it not a secret you can put it in a zip or make a screenshot and share it.

NP BRO! I share my game, i want that the mouses stop when collide with another mouse :frowning: and continue when the mouse walks, u have here my game :slight_smile: ( all the sprites, tiles, animations, all is done by me :smiley:)

** Some variables are on spanish like:

  • vidas = lives
  • atacando = attacking
  • recargando = reloading
  • Disparo = shot
    Raton.gdg (241 KB)

And… i dont know if is correct what i did to substract lives from the carrot :astonished: if u can help me on that too… THANKS!!

I find it difficult to understand what you trying to do in your project since you didn’t used any comments so I decided to come up with a solution for your problem with the collision and stopping the instance and just share it so you can integrate it in to your own project as you see fit.

The trouble is, since you are trying to check collision between the instances of the same object, GDevelop don’t really know which instance you are referring to when you want to stop one of them as at least 2 instance involved in the collision and really there is no easy solution. Somehow you need to identify the instances but even if you are using object variables that’s not going to work because all the instances got the same variable and again, when you try to check the value or compare the value of the colliding instances, it difficult to tell which instance you are talking about and comparing to as both instance got the variable and met the same condition…

The solution I have is basically have an ID variable for each instance with an unique value, and create a collider object for each instance and pass the ID number of each instance to their collider object and basically check collision between the mouse objects and the collider objects and also compare the ID value of the colliding mouse and collider.

If mouse is in collision with collider and the ID variables are different : stop the mouse. 

I have just created an example, take a look and see if it something you can use: (41.2 KB)

For now it does nothing but stop the mouse coming from the left on collision.
As of continue the movement, and reduce carrot life, I didn’t have the time to look in to that but I’ll take a look when I have more time and you can and want to integrate the way I have handled collision and movement of the mouse.

A simple solution which might work is to invert the condition. Rather than stopping a mouse when it collides with another mouse, only move a mouse if it is NOT colliding with another mouse.

In event 25 that would mean ticking the Invert the condition box for the collision check and changing the action from Stop the object Roedor, to apply a force to the object Roedor. As long as you apply a force separately when a mouse finishes eating a carrot and needs to start moving again, this might do what you need.