How do I program an object to automatically dodge incoming objects

  • I’m currently trying to figure out the code for a part of the game I am working on. The idea is you have a car (represented by the Red Block) dodging oncoming traffic (represented by the 1,2,3 blocks) automatically by switching lanes. For example, if the Red block is in lane 1 then it’ll either move to lane 2 or 3 (See second image). To have it move smoothly from one lane to another I have the Red Block “lerping” from one lane to another. The problem I am having is making sure the block moves to the correct lane when a car is incoming so as to not hit the oncoming car.

    The system I have in place right now uses global variables. So any combination of the traffic spawning has an associated global variable. For example, every 3 to 5 seconds the system chooses a variable (2,4,6,8,10,12). I call the variable "Lane Check" in the image below. If it chooses a 10 then the launchers will shoot a 1 Block and 3 Block. Since the variable equals 10 then the Red Block knows to stay in lane 2 to avoid the 1 Block and 3 Block.

    This system sort of works, but it needs the kinks worked out of it or I need to do something completely different. This is where I need help. So if there is any way somebody could help me out that would be awesome. I’ve been fighting with this for while trying all kinds of things. I’ve attached a link to the .c3p file to provide further insight into my problem and give a better understanding. Thanks!

    https://www.dropbox.com/s/6geqdzs9ndpqjgs/Automatic%20Lane%20Change.c3p?dl=0

  • "The problem I am having is making sure the block moves to the correct lane when a car is incoming so as to not hit the oncoming car."

    Looks fine to me, as in the logic you've put in does what it is supposed to do. One thing you could try instead of lerp is give the red block the tile movement behaviour and move up or down in a grid movement. I don't understand what the problem is here.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • What happens when you run the program is, the logic does work, but it can throw the red block into the wrong lane. For example, suppose the program chose global variable 10 like in the picture above. The block will stay in lane two, but before block 1 and 3 make it off the screen the program will switch to another variable because of the "every 3 to 5 seconds", which might throw the red block in the way of 1 and 3. I'm sorry if I'm poor at explaining this, it's a bit tough to explain it in words, but I do thank you for the reply.

  • im not sure if this is what you want, but here it is

    car lanes AI r-140

  • I understand what you're saying but you've chosen to do this by using every x seconds and also adding waits for the traffic spawn events. If you want it a bit tighter then you'll need to replace the every x seconds event with some kind of trigger rather than based on random time, for example when all the traffic is off screen you could run the spawn traffic event again.

  • seems the forum was reversed to the past 24 hrs and some data was lost,

    here is what i posted car lane AI C3-140

    hope is what you are looking for.

  • George this is perfect! That's exactly what I was looking for. Thanks so much for your help. You rock! Also, plinkie thanks to you as well for your input.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)