Herding Mechanic?

Hello,
I am working on a simple game that uses a herding mechanic.

I was wondering how to program AI to herd like cattle and run away from a noise.
If you guys can help me that would be great! Thanks!

I am inspired by the PS2 game “Dog’s Life” and the side quest in the big field.

i’d do it in 2 steps:

  • first have a (mesh-less hidden-in-game, only colliding with the environment, not the animals) “master” ai actor that represents the flock’s “general group behaviour” and does things like randomly wander about or run away from noise.
  • then have your individual animals be ai actors that try to move towards any available point closest to your invisible “flock master” without running into each other (and prioritize points close to themselves, to prevent them running all over the place just to get to a point on the opposite end that’s technically just a tiny bit closer to the center). the EQS should help you with finding this movement target.

then if you want you can even have them have some logic of individually running off if they get too far from their master actor or join another flock, e.g. by having it just do the above behaviour if close enough to any master, and fall back to an individual behaviour. this allows you to spawn or destroy masters whenever you feel like it and the individual animals will react.

you could also instead try implementing rules like “stay away from noise but close by others of your kind” to get the emergent flocking effect, but the above way is usually easier for you to control if you have a way to influence the “group mind”.

also your tags are wrong, this is unrelated to animations or graphics programming.

Hello @nochip
Can you provide some pictures of what you mean please?

if you just need to run in the opposite direction from a noise then its a pretty simple matter of finding the look at rotation, convert that to a direction vector, and using that to find a location to move to. as shown below.

if you are trying to make a herd move as one on hearing a noise then i would pursue nochip’s solution. also look into the kite demo (i think thats the one) which uses a system of herd movement for the deer in the level.

sorry, not sure how i would go about providing pictures of an abstract concept like this, but essentially i’m describing a really basic stripped down version of what Horizon Zero Dawn did, see also null - YouTube for a nice explanation of how their AI works.

tl;dr: your “herd”/“flock”/etc is an actor (just with no physical mesh) that floats around and does the whole “what would the herd do” thing, and your individual animals have AI to just stick nearby the “main herd actor” if they want to (and maybe run off individually and do their own thing or join a new herd etc, depending on the circumstances). that way you can have a really cheap way to influence them all at once, while they can still do their own thing

the best analogy i can think of would be an octopus (i know its weird). ok so the flock control actor mentioned would be like the body and brain of the octopus, it controls the movements of the herd on a macro level. the individual animals would be the tentacles, able to move independently in a limited area/ distance from the main body. ok so now we have animals / tentacles that move around in a given radius of the main actor. when the main flock actor say hears a sound then it alerts the individuals and tells them where to move, this would be analogous to the main body of the octopus moving and since the tentacles are attached they move with it, but in your case the individuals dont move due to physical interaction but rather being told to by the “brain” (flock control actor).

put another way the flock actor represents the herd mentality and controls the group wide decisions and reactions.

except i’d probably let the main flock actor just move there itself rather than actually informing the others, that way the animals don’t have to know when/why/where to move in advance, they just blindly stay close to it. might be enough, but keep the “send a message to the animals” option in mind in case you need something more in depth.

true i was just thinking in the case of say a predator approaching then you would want the herd to move immediately and not have any delay (finish current move to then notice oh im too far from flock). instead of having the animals constantly checking if out of range you just pass a message saying we’re on the move. could also be done with overlaps too.

not have any delay (finish current move to then notice oh im too far from flock)

“Observer Aborts: Lower Priority” :wink:

instead of having the animals constantly checking if out of range

you should be doing that from time to time anyway though to prevent them literally just brownian motioning away from the flock :stuck_out_tongue:
also in a 3d game full of collision and realtime graphics etc a simple abs(myLocation-flockLocation)<maxDistance every few frames is probably negligible overhead.

then again yeah a system where both the animals inform the herd to panic and the herd broadcasting where to panic away from, plus a bunch of other channels (animal “i’m hungry” → herd broadcast “let’s wait around that berry bush”, etc), might be useful for more advanced behaviour, but since the OP doesn’t seem to have a whole lot of experience in this kinda thing they might wanna start small with a simple “you walk over there and everyone else stay close” and improve from there.

i wasnt even thinking as advanced as behavior trees or anything like that lol what i had in my thinking was just a simple get random point in radius (origin = flock actor) then a ai move to. do the random move for both the flock actor and the individuals (have their location based on origin = flock actor as well).

in any case id say theres many ways to accomplish the same thing so both ways of approaching the situation would probably work depending on the size and scope of the game.

nah i’d always use a behaviour tree for that kinda thing, you don’t want to code the AI by hand :stuck_out_tongue:

especially since it’s probably gonna be a bit more complex behaviour than “just” a flock of birds in the background or something

Alright, yes it is the Kite demo that uses deer. I looked at the event coverage video of it that explains what they did but not how they did it.

Also with the photo (capture.png) is that suppose to be put into the “master” AI actor blueprint?

the picture i provided was not really meant for use with the solution nochip mentions, it was a separate thought so to speak. i was just showing how you could get a location in the opposite direction of a stimulus source. so a similar methodology could potentially be used with the nochip solution but it depends on how you plan to implement you system.

the above implementation was build on a different interpretation of herding. herding could be taken as moving things in a group or herding as in what a dog does more like corralling i suppose. i posed the above as a alternative to nochip’s solution in case you had meant herding in the latter context.

Ah, okay
Yes I believe I would like to corral.
Nochip’s approach was a little complex for me, I want to have the cattle move away

Here is a video of the gameplay and basically the mechanic I want. I looked at another video [Run Away Cube][2] that has what I am looking for although I find that the detection is too big for my map and I want it to be a little closer in order to activate the run away mechanic and also it clips through the trees and the fence.

the picture shows the basic idea of getting the look at rotation and and using that to get a vector direction to find a location. if you want a more real time movement then you could use a add input node on tick as long as you have a movement component. using input movement or a ai move to or simple move to will get rid of the clipping issue.

it can still get quite complex though. below is a little example of mockup i was working on which in the end didn’t give a great result. it was done in a pawn using a floating movement component. also for the overlap i had a large collision sphere on one of the actors, either the dog (third person char) or the sheep. the collision sphere if basically the same as the detection radius in the tutorial you posted, if its too big then just make it smaller.

it may actually be worth learning a bit about behavior trees as that may be cleaner in the end.

Thanks for the suggestion, unfortunately it didn’t work. After running the game I would get a lot of errors with my AI_Cattle_BP.

I tried using behavior trees before but it didn’t work(?) My AI_Cattle_BP would not work with it.

ok… how was it not working? describe what it was doing and it was meant to be doing? show what you’ve done so we can see what you have and where things could be going wrong. what did the errors say?

remember that we are here to help you by pointing you in the right direction and giving you more information to work with. we are not here to do the job for you. so this becomes a discussion, both sides participating. by just saying “it didn’t work” isn’t helping anyone, its like going to a doctor and saying im sick. without more information and specific information we cannot give a good response.

i see you attempted to copy the example i posted almost exactly. i did mention that it didn’t work great so it would need further work and was just to provide the idea.

anyway even if you were attempting to copy it you have several issues is see off hand. first you have cattle overlapping cattle which doesn’t seem like the thing you want. you instead want to be checking overlaps of a different class, in my example i used dog (third person bp) as the player pawn and sheep as the thing being corralled. next on end overlap you are setting the variable again when you should be clearing it (setting it to nothing). if you never clear the variable then it will always be valid. which brings us to the next issue, for the move to event you are trying to run the move to if the variable is valid when it should be run when the variable is not valid. the last issue i see right now is that you are getting a random location in radius of 0.0 which will result in the sheep not moving anywhere.

i should explain a bit more on the example i was working with. first the idea was that when the sheep was not in range of the player then it would roam about, when it was in range then it would run in the opposite direction of the player. everything connected to tick was to make the sheep run from the player. the on begin overlap script was used to increase movement speed and allow the running to begin (enable the tick script to function). the end overlap decreased the movement speed, stop the tick event (stop running), and to begin the new behavior of roaming (moveToRandom). the move to random event served to do just that, get a random location and move the sheep there. it was meant to only function when not overlapping the player hence if dog (variable referencing player) was invalid then we know we are not overlapping the player and we roam.

the errors you have generally result from getting the value of a variable that isn’t set so addressing the above will eliminate some of that.