Game Development Reference
In-Depth Information
2D position on the level with a constant height. We'll be using this method in several
other spots in the code.
Note
We could do additional checks on this position finding to make sure that the spot
never overlaps any of the walls in the scene, but to make the code simpler for
this demo, we won't worry about this edge case. However, you would do this in a
production game.
Reacting to game events
Next, wewant to havesomeshipschasegold pieces, but we'll makeit moredynamic
than in the last demo. Create a Sphere object with a gold color and add a RAIN en-
titytoit(bynavigatingto RAIN | Create Entity )andadda Visual Aspect called Gold
to it so that AI characters can sense it. Turn this gold piece into a prefab. Instead of
just placing it manually in the scene, we want them to be spawned randomly; add the
code mentioned in the following screenshot to the Ground script:
In the Unity editor, drag the Gold prefab to the Transform gold in this script. This
script randomly spawns a gold piece somewhere in the level every 2 seconds by
tracking the time using Time.deltaTime . If you run the game now, you'll see a gold
piece created randomly every 2 seconds. Next, we need ships to collect these.
Our AI ship characters will pick a random spot on the level and travel there and then
after arriving, pick another random spot to go to; however, if they see a piece of gold
along the way, they will stop and pick it up. To do this, create a ship object with a
RAIN visual sensor with a horizontal angle of 120 , a vertical angle of 45 , and a range
of 15 . The behavior tree for the ship will be straightforward. Set the root node to par-
allel and one Detect child set to look for Gold and store its form in the gold variable.
Add another child to the root with a constraint to test if gold == null . If gold is not
null , it should move to pick up the gold; if it is, pick a random spot on the level and
Search WWH ::




Custom Search