Hi, I have about 5 NPCs randomly moving around in the environment.
Should I use character controller or rigidbody collider for the NPCs?
Hi, I have about 5 NPCs randomly moving around in the environment.
Should I use character controller or rigidbody collider for the NPCs?
If they need to have realistic physical behaviour, use rigidbody+collider. If they need to run incredibly fast and then stop suddenly, use a CharacterController.
Hi,
How do you get random movement of NPCs? I’ve been trying to find or come up with a simple script for this, but can’t seem to get it to work. Making them move randomly but not erratically is trickier than I thought. :roll:
Thanks for the help.
Nah, just randomly pick a direction within maybe 45 degrees of current heading, move that direction for x seconds (clamped so it’s not unrealistic), then repeat. Detect collisions and adjust accordingly.
How do you randomize the angle? In other words, you don’t want a 45 degree turn each time. Also, how do you keep the NPC from leaving the map? If you have walls then he/she/it will hit the wall and stay until the new turn is initiated, but what if there aren’t walls? I’ve had a couple of suggestions on another forum posting, but just want to see how you handle it. Picking a random point within a certain perimeter and translating to it or putting up an invisible mesh and collider around the perimeter are the two methods that have been suggested.
Also, I’m not sure what “clamped” means. I’ve heard this term a lot and still don’t know the definition. Sorry for the noob questions and thanks so much for the reply.
The two methods you mentioned are both good ones. The Random class will be your friend, and you can use Mathf.Clamp() docs for examples. Clamping is where you keep a value from going higher or lower than given end values. For example, if x is clamped between 1 and 45, you will always get an x value between those values – it would prevent REALLY erratic movement, but still allow some variation.
Thanks yet again. I appreciate the clear explanation. I’ll check out the scripting references and see if I can put this all together.