I have been trying and trying to get this enemy to switch direction when he touches a wall and go the opposite direction. I believe how I have it should work but it is not, and I got no idea why I been trying figure this out and just cant XD
Please take a look and just press p and notice how moves doesnt switch when hits that wall, and if can make a give a explanation or a fixed version to me please I just cant figure it out.
The problem is that you change the direction every time a collision is detected. Collision is detected multiple times when 2 objects are colliding so it changes the direction many times and looks like its bouncing.
If you’re not going to do any more advanced AI, I would recommend just adding an IPO of it moving back and forth. If you absolutely can’t do that, You could use a property assigned in the script that checks if it has turned recently.
if own[‘turned’]==True and collision.positive==False: # If has turned and is not touching wall…
own[‘turned’]=False # Turn off the has turned marker.
if own[‘turned’]==False and collision.positive==True: # If haven’t turned recently and are also touching wall…
reverse_direction()
This will refuse to reverse direction beyond the first time, and will reset it once it receives a negative pulse (is no longer touching wall.)
@mercc I would not recommend using an IPO, as this would stay the exact same when you duplicate an enemy (he would move back and forth on the same position as the first one). You may be able to use the relative location channels of the IPO, but I never really liked those.