Car 'traffic' - maintaining distance to each other and player car

0 favourites
  • 7 posts
From the Asset Store
Basic Rounded Vector Geometry Player Design with Glow for 3 player games
  • Hello,

    I'm trying to make car traffic, but don't understand how to code something.

    I'm trying to make the cars 'interact' with eachother and the player car.

    See:

    The top pink car is the player car. The two cars below are 'CPU' cars. When I drive, the cars should too. But always maintain distance to the player, and to the other CPU cars.

    I've tried different things, but can't figure it out.

    See:

    The CPU cars are bullets and drive 50 speed automatically, except when they encounter another CPU or player car it should stop when anything in front of it is stopped too. The player car has car behavior.

    Also, the cars are all in one sprite, except the player car. So the appearance of the CPU cars are already randomly generated.

    Thanks.

    EDIT: How to I show the uploaded image to the forum?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • OK, so I tried something new:

    This works, but only if there isn't the same car behind eachother on a lane.

    Example:

    PlayerCar

    Car1

    Car2

    Car3

    Car4

    Car5

    Works fine.

    PlayerCar

    Car2

    Car2

    Car3

    Car4

    Car5

    If the PlayerCar stops at the second example, the second 'Car2' in the row will drive through the other Car2.

    That's the problematic thing with the code I posted above.

    I tried to minimize the code and brought it down to one PlayerCar and one Cars sprite. But the result is the same; if the same instance is put or spawned behind eachother, the bottom Cars sprite will drive through the other and simply not 'listen' to stop for the same Cars sprite. It will stop for the PlayerCar however.

    Thanks.

  • You need to add all car sprites, including PlayerCar, to a family. Actually, you need two identical families - Cars1 and Cars2.

    This will allow you to optimize your code:

    For each Cars1
     Cars1 is overlapping Cars2 at offset (0,-50) -> Cars1 set Bullet disable
     Else Cars1 set Bullet enabled
    
  • Hey, thanks for replying!

    I implemented the code you provided, however, the PlayerCar also moves automatically since it belongs to a family with the bullet behavior.

    Is there anyway to exclude the behavior from the family for the PlayerCar? I also need to exclude the solid and wrap behavior then.

    Because the PlayerCar needs to stay player controlled.

    Regards

  • You can simply disable Bullet behavior for player's car. You can also exclude it from the "For each" loop using some condition, for example "Cars1 UID not equal <player's car UID>".

    Anyway, I made a little demo, maybe it'll help.

    dropbox.com/s/qnkaoezzbwp203g/CarsDemo.capx

  • Hi,

    Thanks very much.

    That demo looks really good.

    I tried implementing your example attached .capx in my file but I could not get that to work though. I tried exactly replicating your example to my file.

    The cars in my file would still keep overlapping, but your demo it all works fine.

    I managed to code something with a little extra coding and now the traffic works.

    I also disabled the Bullet behavior for the Player it's working now :)

    Now my cars use the wrap behavior, which works fine, except that the wrap behavior doesn't take into account that it should stop for other cars.

    You see, the car must first be fully out of the screen area, before it enters it from the bottom.

    So if I don't drive forward, the cars that are wrapping on the top, will bunch up on the bottom and overlap eachother. I guess I could add extra cars below the existing lanes and remove the wrap behavior, but I was wondering if wrap could still be used to save time and memory space.

    Any ideas?

    Regards

    EDIT: I think it can be done with adding 200px to the Y of the car that is overlapping. Or going to overlap. Tried some coding, but so far it's giving me weird results or the cars still bunch up.

  • Since cars are moving in one line, you can simply check Y instead of using "overlapping at offset". So you can do something like this to maintain 100px distance between cars:

    For each Cars1
    
     Cars2 compare Y<(Cars1.Y)
     Cars2 compare Y>(Cars1.Y-100) : Cars1 slow down
    
     Cars1 Compare Y<50 
     Cars2 compare Y>(LayoutHeight-50) : Cars1 slow down
    
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)