Move character to random location on navmesh

Hi, probably a simple thing to do but im new to game engines.

I want to have a computer controlled character to get a random location on the navmesh and move to it every few seconds or so.

Thanks in advance!

Do do it so it goes randomly to truly any position on the navmesh with logic nodes would be difficult in my opinion, although I’d be very happy for someone to prove me wrong! :slight_smile: I would suggest that an easier way would be to have a collection of empties at various points, which are all in put into an array with the Set Array node, and then you use a Random Integer node to get a random one of them back, get it’s location and spawn your character at that location.

In this way you can have a collection of as many random spawn points as you wish.

There are other ways with other limitations, such as if it’s ok for the character to spawn anywhere in a flat rectangle, you can just plug random integers into a set location node. If it’s ok for them to spawn anywhere in a circle (of varying height), you could plug two random integers into the rotation of an object, which then uses a Cast Physics Ray node to cast a ray onto the navmesh and wherever the hit location is, the character spawns.

1 Like

maybe you could use the GetMeshNode and for example use the z coordinate in your mesh data that corresponds to the same x,y of your object.

Interesting… I’m not familiar with GetMeshNode (and I don’t see it mentioned in the docs). What does it do? Return an xyz coordinate on a mesh surface? How does that work in something like a sphere where there may be more than one point that corresponds with the x and y and different depths of z?

I could more-easily describe how to do this in “straight Haxe” than to describe how to do it in logic nodes … but meanwhile it does occur to me (as Armored_Blob has obliquely suggested) that the actual algorithm that you want probably isn’t going to be, “just random selection.” You don’t want the movement of your bad-guys to resemble a Lottery drawing.

The algorithms that are often used in actual games – as early as PacMan, believe it or not – are often based on path-finding: “yes, that bad-guy is coming to get you!” I’m sure that this is beyond “logic nodes,” although it is not beyond procedural Haxe programming. (The total programming ecosystem of Haxe includes a number of path-finders.)

If i remember you will find in the node code something called data … need then to look in iron.
For z, it’s up to you to set a logic like the nearest z …

Thanks, i was actually looking for way to move character to random locations rather then spawning in random locations, but i guess your answer works for both :slight_smile:

thanks! but i think i kinda want my bad guys to move to just random locations, lottery style :wink: actually they are not bad guys, just random creatures that i want to roam the world randomly. trying to use game engines in a more artsy fartsy way to create conceptual worlds and interactive installations. And noise and random is a big part in my “art” :slight_smile:

OK, so you know what, you could actually use the method I mentioned above about rotating an object a random amount on two axis, so it can be pointing anywhere in a rough circle… then use raycast to detect if it’s pointing somewhere on your ground mesh. Then all you do is make it run again if it isn’t pointing at a point on the navmesh. When it finally is… you spawn there.

The only real limitations I can see are that you can’t guarantee that the creature will appear exactly the first time you run it, however the code can iterate fast enough that you’ll get a hit before any human will notice. Also, if your navmesh has different levels, say, ground going under a bridge, then it’s always going to appear on top and never underneath (ad the raycast will hit the top part first).

Other than that, it should be able to do what you want.

2 Likes