# 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.

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! 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

thanks! but i think i kinda want my bad guys to move to just random locations, lottery style 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â€ť

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