x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

How do I move simple AI in a 2D space?

I've been looking all over for a solution that works and maybe it's so simple that thats why I can't find anyone talking about it but I have my little 2D AI BP character who I would like to make move around. However the 'simiple move to location' function in BP is for nav meshes and using it in a 2d space doesn't seem to work.

Whats the best/simplest way to get a 2D character to move to a new location? Specifically which function in BP.

Thanks!

Product Version: UE 4.10
Tags:
more ▼

asked Dec 29 '15 at 06:31 PM in Blueprint Scripting

avatar image

SpaceBearDev
21 5 9 12

(comments are locked)
10|2000 characters needed characters left

1 answer: sort voted first

There are a lot of "it depends" type answers that could be given here, depending on if your AI is like a physics driven ball, or a character-movement driven character (instead of a pawn) or maybe even a paper 2D sprite in screen space. But the overall techniques come down to two main choices:

  • You can use a nav mesh and "simply" tell the AI to go to a location.

  • You can teach your AI (or it's controller) how to drive towards a destination.

So given you said you're having a problem implementing a nav meshed solution, here's how to do the 2nd way.

  1. Moving somewhere happens over time, so this is a clue to implement the following in your Event Tick function.

  2. First step is to figure out which direction you want to go. Directions are expressed as vectors. The best way to get an arrow pointing in the direction towards where you want to go is to subtract the destination position from your AI's current position.

  3. The length of the arrow formed by that subtraction will depend on how far away the target is. This isn't necessarily a good thing for movement purposes. So to shorten it up to be 1 unit long no matter how far away the target is, you'll want to "Normalize" that vector's output. Think of it like a compass needle - its always the same length pointing north no matter how close to the north pole you are.

So that's how you steer. To hit the gas, you'll want to have a variable you can play with to adjust your Speed. This should probably be multiplied by the Delta Seconds coming from the Tick event so that games played on computers with higher frame rates don't see the AI going faster.

Depending on what type of movement input you want to use, you'll now have all the pieces to get your AI to move in the correct direction towards a goal. The "Add Movement Input" node takes a Scale Value (your gas pedal) separate from your Direction (your steering). The "Add Input Vector" combines them both (by multiplying them).

In this example, the goal is a target actor stored in a variable I made called "My Target". So this would work if you wanted the AI to move towards the player for example. But you could also do this so it goes towards a specific location or in a specific direction by feeding it a vector instead of using My Target's actor location.

Not too hard once you've done it once or twice - hope this helps.

alt text

more ▼

answered Dec 29 '15 at 11:00 PM

avatar image

1l2Hawk
690 36 35 74

avatar image SpaceBearDev Dec 30 '15 at 10:04 AM

Hey thank you so much for the reply, that was a lot more detail than I could've asked for. I tried the first method and it worked perfectly on the first try!

I should be able to get the rest of the AI stuff working as it's already working for the players character, I just couldn't find any example of what you have shown me. Thank you again for the help, my little town has functioning citizens now :)

avatar image bizortz Aug 05 '17 at 03:42 PM

I have the same problem and after read this post, I'm still confusing about the "My target" node. How can i store the value of player in another bp to this variable?

Thanks alot!

(comments are locked)
10|2000 characters needed characters left
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question