Friday, January 18, 2013

How to create Wandering Animals in your Jibe world using Unity 3D Pro


A great way to breathe life into your Jibe world is to add animated animal models that wander around the 3d landscape.  Here's how you can do it using the Pro version of the Unity3D editor.
The Unity Asset Store has many great models available for low cost that can make your Jibe space come to life. Just run a search for Horse or Cow and you may find just the model you're looking for. To make roaming animals, look for animals that come complete with animations. Mixamo produce many models that work well in Unity, though there are plenty of other publishers you can choose from. For this simple ambient animal example, you need just two animations - one for a stationary animal (so an idle / grazing animation) and a walk or run.
Step 1 - place animals and place empty cubes that the animals will walk to.  If you want the cubes to be specific per animal than you can place them in the animal object in the Hierarchy for organization but it doesn't matter where they are in the Hierarchy for performance. Making the cubes specific to an animal (by dragging those items into the hierarchy of the animal) then you can move the animal and the relative waypoints together in edit mode more easily.
Step 2 - Set up the Nav Mesh. to do this go to the Windows menu and open the Navigation window located towards the bottom of the list
Next select from the Hierarchy any objects that you want the animals to avoid such as buildings, fountains etc. In the Navigation window under the Object Tab those objects will appear with two check boxes and a drop down. Check off the Navigation Static check box to make it true, this will prevent the Nav Mesh from going under or through these objects. Next select the Terrain from the Hierarchy and check both Navigation Static and OffMeshLink Generation
Now in the Navigation Window click on the Bake Tab located next to the Object tab (do not press the Bake button at the bottom of the Window yet).  These settings will determine the accuracy of the Nav Mesh:
  • Radius - how close to walls the Nav Mesh will generate.
  • Height - how much clearance is required for the Nav Mesh to go under a floating object.
  • Max Slope - the steepness of a slope that Agents can traverse.
  • Step Height - the step-up height Agents can traverse between levels.
  • Min Region Area - the smallest amount of space that the Nav Mesh can exist in.
  • Width Inaccuracy % - how inaccurate the horizontal distance from wall and ledges the Nav Mesh is.
  • Height Inaccuracy % - how inaccurate the vertical distance between the terrain the Nav Mesh is.
The Generate Off Mesh Links are for special actions of Agents being able to jump across gaps and drop down from ledges.
When everything is set up the way you want it click the Bake button located at the bottom right of the Navigation Window.
Step 3 - setting up the Animal AI. Download RGScripts.rar (includes AutoAnimateNPC.cs and NavigateToTarget.cs).  The below picture are the four scripts that will need to be on the animal to allow them to walk around.
  • Animation - Set up the animations that the Animal will be able to do.
  • Nav Mesh Agent - The most important of the scripts, this is what attaches the animal to the created Nav Mesh. Most of the settings in here are straight forward, the Radius is a collision sphere that tracks where the animal is on the nave mesh, speed is how fast the animal will move, height is how tall the Nav Mesh collision sphere is (so if there is a bridge that can be walked under and the height is set too high the animal won't be able to get under the bridge.
  • Auto Animate NPC - plays the correct animations when walking or idle.
  • Navigate To Target - sets the targets that the Animal can walk to.  This script will talk to the Nav Mesh Agent selecting a random destination from the list and passing that position to the Agent.
When you are happy that all works as it should, make all the targets invisible by turning off their Mesh Renderer component and removing their box collider components so you won't end up bumping into invisible boxes as you walk through the scene.
Step 4 - Run the game and enjoy.

-Matt Bertrand
Jibe Developer, ReactionGrid

1 comment:

  1. That was the COOLEST!!! We will keep on eye on you. From friends from Brazil,thank you very much!!! If you pop in this side of the planet lets us know. Cheers.

    ReplyDelete