[SOLVED] How to do obstacle avoidance AI?

Hi, i’m a beginner with Defold and i try to make my first game with it.
My game is a one way platformer game (like mario run) and my probelm is that i don’t know how to create a simple AI enemy who avoid the trap etc… I already try some way with 2 trigger attached to my enemy and when the trigger enter in collision with an “danger” objet it send a message to my enemy and the enemy go to the opposite. But it doesn’t work !
There is my trigger script:

function on_message(self, message_id, message, sender)

	if message_id == hash("trigger_response")then
		if message.group == hash("danger")then
			msg.post("mechant/mechant#script", "triggerD")
		end
	end
end

and here is a part of my enemy script :

function update(self, dt)
	self.velocity.x = -speedX
	if triggerG == 1 and self.velocity.x == -speedX then
		self.velocity.x = speedX
		triggerG =0
	elseif triggerD == 1 and self.velocity.x == speedX then
		self.velocity.x = -speedX
	triggerD = 0
	end
...
function on_message(self, message_id, message, sender)
    if message_id == hash("contact_point_response") then
        if message.group == hash("solide") then
            handle_geometry_contact(self, message.normal, message.distance)
        elseif message.group == hash("danger") then
        	self.velocity.y = 400
        	speedX = 10
        	msg.post("#collisionobject", "disable")
        end
    elseif message_id == hash("triggerG")then
    	triggerG = 1
    elseif message_id == hash("triggerD")then
    	triggerD = 1
	end
end

I haven’t forget to add “trigger” and “danger” in my enemy object and danger object so i think the problem is in the script.

here’s my enemy collection with the trigger left and right and the enemy

(I’m sorry if i have do some mystake i’m not English)

I’m just point you to one great example:


http://britzl.github.io/publicexamples/simpleai/index.html
Sorry, can’t check your code, I’m also just starting with Defold.

1 Like

Could you tell me a little bit more about the behaviour that wish to create for the enemy AI?

It’s a platform game right?

Is the enemy only moving left/right?

What should happen when the enemy gets close to something dangerous? Should it move in the opposite direction? If, so, for how long/which distance?

Oh, I see now that you flagged the topic as solved. Is that correct?

1 Like

I have find an other way to do it, i add at my “danger” object a trigger that only react with the enemy then when the enemy is in the trigger i just change

self.velocity.x = -self.velocity.x

so he go to the opposite x direction.
But i have just one other question @britzl , when i add a second enemy it move at the same time the other move. I mean he change left/right without touching himself the trigger. So how to do a variable unique to each enemy ?

You should store variables on the selfobject/table. Based on the code in your original post there seems to be a couple of global variables such as speedX, triggerD and triggerG

1 Like