Friday, September 4, 2015

Basic MoveTo Behavior For Vehicles In Unreal Engine 4

Baldur's Gate, MechCommander, Neverwinter Nights, Dragon Age: Origins and Pillars of Eternity: games featuring squad-level real-time with pause tactical gameplay.  Those beloved franchises balance a mix of RPG micromanagement and battlefield strategy, but there's a void. 

As a fan of Car Wars, The Road WarriorSpy Hunter and Death Race, I want to arm and armor a squad of post-apocalyptic muscle cars then send them into the wasteland to challenge gangs of jury-rigged monstrosities-on-wheels fighting over gasoline and spare parts.

Image from imdb.com
This.  A screen full of this.

Engines like Unreal (and Unity) are available for free and it's easier than ever for inexperienced developers to create games.  With no eagerly anticipated announcements forthcoming, like any smug mature gamer with a programming background I thought, "How hard could it be?"

As we're going to find out, fairly hard.

"Simple" Vehicle Movement

The first feature to tackle is point-and-click movement.  Instead of directly controlling a single vehicle, we want a squad of vehicles to drive themselves to a specified destination.

I assumed it would be as simple as importing a vehicle from one of the examples, build a Behavior Tree Task and send it a MoveTo.

Done.  Next on the list?  Guns!

Except that pawns using the WheeledVehicleMovement component don't respond to MoveTo.  They respond to Throttle, Steering and Handbrake.  So not only do we have to tell it where to go, but how to get there.

The typical approach to racing game AI is to pre-build waypoints into a circular track.  That won't work for this game due to the open-world style maps.  We need to tell a vehicle to move from anywhere to anywhere.  Fortunately, we can still use Unreal's NavMesh intended for character pawns.  The FindPathToLocationSynchronously function simply needs Start and End vectors.  The result is a NavigationPath object, which includes an array of Path Point vectors.

Who says A* Pathfinding is complicated?

Now we can build a short Behavior Tree Service that recalculates a path from the vehicle's location to its destination.  Finally, have the vehicle always steer toward the next waypoint, NavPoints[1], and set the throttle to 1.0 (for now).  Our logic so far looks like:

I thought you said this was hard?

Here's what it looks like in-game:



Next time, we'll make the throttle smarter and make use of the handbrake.


9 comments:

  1. Hey bro, do you can to explain me how you did it, please? Or give me an example of logic. I make strategy, and I can not configure the behavior of inorganic units, vehicles for example. I searched all the internet and found you :) Sorry for my english, it's not my native language, very hope on your help. Many thanks and good luck.

    ReplyDelete
    Replies
    1. Which part are you having difficulty recreating?

      Delete
  2. Hi!
    Care to show how you calculated the steering, I presume that is the part with which most people have difficulty when recreating the behavior.
    Thank you and have a Happy New Year! :)

    ReplyDelete
  3. The initial steering calculation came from Peter L. Newton's Unreal Community post on AI: https://forums.unrealengine.com/showthread.php?25073-UPDATED-5-16-A-I-Templates-Bot-Car-amp-Flying-AI .

    Here's a snapshot of the blueprint I use: https://picasaweb.google.com/lh/photo/xYK150zyY5bxbSZTv2i5GOdlRTwEM_67xEbSe2CuYpI?feat=directlink .

    ReplyDelete
    Replies
    1. hi there. the link seems to be broken.

      I'm also having trouble dealing with steering behavior. I'm using Peter newton's one as well.

      Delete
  4. Will this setup work for physics based pawns, (pawn that moves by the help of "addforceatlocation" and "addforce"..)?

    ReplyDelete
  5. Your vehicle AI blog posts are extremely helpful. I do have one question; How do you find out what the max wheel turn angle is for your Vehicle Movement Component? All I know is that the max steering angle changes depending on speed.

    ReplyDelete
  6. Hi! I'm also trying to do AI vehicles. I'm encountering a problem with NavMesh.
    How did you manage to get a route points with offset from walls?
    You have used NaveMesh modifieres or sometheng?

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete