1
\$\begingroup\$

As of now, to get random movement of my enemies, I am adding a Rigidbody and a BoxCollider then "throwing" them (i.e. adding velocity) in a random direction for a random amount of time and making them choose a new direction if ever they get outside of their designated area. This does not seem to work that well all the time. Sometimes they manage to get themselves stuck inside obstacles which should be impossible. The more I add guys, the more it seems the physics can't keep up or something with the collisions. Is there a better method to random movement or is "throwing" them perfectly valid? Is my method expensive and terrible? I might have around 50 guys active at a time and perhaps a few hundred to a thousand static obstacles each with their own colliders.

\$\endgroup\$
  • \$\begingroup\$ Are you making sure that the "new direction" doesn't just get them further towards/into whatever they are colliding against? \$\endgroup\$ – McAden May 4 '17 at 22:14
1
\$\begingroup\$

Throwing them is perfectly valid and it is not a bad way of doing things unless you find that it becomes an issue. The amount of collisions you are describing should not be too expensive unless your Colliders are complex and you are running on a platform that has limited resources.

The "getting stuck" part is the bigger concern. There are a number of reasons that could be happening. You might can avoid this by changing the collision detection on the objects' rigidBody to Continuous instead of discrete. You normally wouldn't want to do that unless the objects are moving extremely fast because continuous does have a higher CPU cost.

\$\endgroup\$
  • 1
    \$\begingroup\$ Turns out I had two different issues with getting into walls and obstacles. Neither was physics-collision-based. One was simply the rotation point (origin) of my object being off-center so when they turned around to pick another direction, they first go right through the wall! The other was that my guys were appearing before the obstacles and thus they'd move where obstacles later just appear on top of them. Good to know that my method doesn't immediately cause mocking and jeering though. Must not be too awful a method :-) \$\endgroup\$ – CodeMonkey May 5 '17 at 1:33
0
\$\begingroup\$

You can build a Navigation Mesh of your movable area and send your agents to arbitrary positions inside its bounds.

\$\endgroup\$
  • \$\begingroup\$ I think that requires a terrain, right? I'm not actually using a terrain for my game. Either way, how would I avoid choosing a random location where an obstacle is placed? Is there some kind of "if x and z have valid navmesh in y range" check? \$\endgroup\$ – CodeMonkey May 5 '17 at 17:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.