• No se han encontrado resultados

El Movimiento Obrero y la Marina Mercante Panameha

EL TRABAJO MARITIMO EN LA EVOLUCION DEL MOVIMIENTO OBRERO PANAMENO

D. El Movimiento Obrero y la Marina Mercante Panameha

{ if(volume==volumeON) fadeState=1; } else { if(volume==0) fadeState=0; }

A quick interpolation between the current volume and the targetVolume, with the time it takes to fade decided by Time.deltaTime multiplied by fadeTime:

volume=Mathf.Lerp(volume, targetVolume,

Time.deltaTime * fadeTime); source.volume=volume; }

}

The last two functions in the MusicController class set up the fader variables for either fading in or fading out through code. The single parameter fadeAmount is a value for which to decide how long it should fade in or out:

public void FadeIn ( float fadeAmount ) { volume=0; fadeState=0; targetFadeState=1; targetVolume=volumeON; fadeTime=fadeAmount; }

public void FadeOut ( float fadeAmount ) { volume=volumeON; fadeState=1; targetFadeState=0; targetVolume=0; fadeTime=fadeAmount; } }

8.3 Adding Sound to the Weapons

There are two places that may, at first glance, make good sound trigger points for weap- ons. One is when the fire button is pressed in the player script, and the other is when the projectile itself is created.

Making a sound when the fire button is pressed may seem like a good idea, but to do that, we have to make sure that the projectile actually makes it into the world. After the fire button is pressed, it may be that there is no ammunition left in the weapon or that the weapon is in its reloading period. Making the sound without firing the projectile would be rather silly.

Making a sound when the projectile is spawned may in fact be a better option, as it will only make a sound when a projectile is successfully made. Also, the sound can be

tailored to suit the projectile. For example, perhaps a small laser blast has a less powerful sound effect than a larger blast, or perhaps a green laser sounds different from a red laser.

In the example project for this book, the code to play a firing sound from the pro- jectile will already be in place. The code resides within the ProjectileController.cs script.

Note that placing the call to play the projectile sound in the Awake() or Start() func- tion may cause problems because of the time it takes to initialize versus the time it takes for the script creating the projectile to get around to position it correctly in the 3D world. To ensure that the projectile will have been positioned correctly when the audio is played, this code goes into the Update() loop with a simple Boolean flag to stop the sound getting played more than once.

The variable declarations related to the firing effect, to go with the other declarations at the beginning of the script, are as follows:

private bool didPlaySound;

private int whichSoundToPlayOnStart= 0;

The code to play the sound is placed at the top of the Update() function. It checks first to make sure that the sound has not already been played and then makes a call out to the BaseSoundController static variable Instance. The PlaySoundByIndex parameters are the contents of the integer whichSoundToPlayOnStart variable and the projectile transform’s position. After the sound call, didPlaySound is set to true to prevent duplicate calls to play:

if(!didPlaySound) { BaseSoundController.Instance.PlaySoundByIndex(whichSoundToPlayOnStart, myTransform.position); didPlaySound=true; }

169

9

AI Manager

When computer scientist John McCarthy coined the term artificial intelligence (AI) in the mid-1950s, I doubt anything like the path-finding killer robot cars we will be building in this book would have been on his mind.

The AI Controller in this book is a basic state machine, focused on function rather than on any kind of emotional simulation. For it to work for the example games later on in the book, our AI controller will be able to perform several different actions. It will need to be able to control a vehicle (as in a body, a spaceship, or other moving entity) by turning and moving forward and backward and using vision to try to avoid obstacles. When we need the controller to follow a specific path, it should be capable of turning toward and moving in the direction of waypoints.

Achieving this will require the controller to act differently in several different states. The BaseAIController.cs states are as follows:

1. moving_looking_for_target

Moves forward, continuously checking the distance between this object and the target. If the distance is below a set chase distance (maxChaseDistance), then the AI uses raycasting to check the line of sight between object and target. If the target is visible, the AIState changes to chasing_target.

Two “feeler” raycasts will be made out from the front of this object, looking for obstacles. If an obstacle is found on the right, the AIState changes to turn left. If an

obstacle is found on the left, the AIState changes to turn right. If obstacles are found to be on both sides, the AIState will change to backing_up_looking_for_target. The AI will back up until it no longer detects any obstacles.

2. chasing_target

The AI will turn to face the target and move forward toward it. If the target falls out of range, the AIState returns to moving_looking_for_target.

3. backing_up_looking_for_target

In this state, the AI moves backward until no obstacles are found by its for- ward raycasting. When no obstacles are found, the AIState switches randomly to either stopped_turning_left or stopped_turning_right. This is randomized to help reduce situations where backing up and turning in a particular direction are unsuccessful in getting the AI away from an obstacle. Repeated backing up and turning should eventually free the AI. As it does this, the code still checks to see whether its target is within range (and if it is, the AIState will change to chasing_target).

4. stopped_turning_left

The AI will turn to its left until no obstacles are found by the forward raycasts. Although this state means that the AI is not moving, the code checks to see whether its target is within range (and if it is, the AIState will change to chasing_ target instead).

5. stopped_turning_right

The AI will turn to its right until no obstacles are found. If its target is within range, the AIState will change to chasing_target.

6. paused_looking_for_target

The AI does not move in this state but continues to check to see whether the target is within range to chase.

7. translate_along_waypoint_path

Waypoints (managed by an instance of the Waypoints_Controller.cs script com- ponent, discussed in Chapter 7) are used as targets to aim for. Transform.Translate is used to move the AI around, and the BaseAIController.TurnTowardTarget() is used to control rotation. The AI will move forward toward a waypoint and move on to the next waypoint when it gets close to it.

8. steer_to_waypoint

This state is similar to the translate_along_waypoint_path except that it does not use TurnTowardTarget() for steering. Instead, input values are calculated (in the

171 9.1 The AI State Control Script

same format as user input values would be) so that a player controller script can use them and deal with turning itself.

9. steer_to_target

This state is similar to steer_to_waypoint except that it does not have any way- point checking incorporated into it. Instead, it will provide input values for steer- ing the AI toward its target. There is no obstacle checking in this state either, only steering.

10. paused_no_target

In this state, the AI does quite literally nothing. It does not move or look for a target.

As the BaseAIController.cs script encompasses so much functionality, it’s is one of the biggest, if not the biggest, in the book; there are over 50 lines just in the variable dec- larations! It may seem rather intimidating at first, but remember that not all of this script will be running at any one time. Within the main Update() function, we only actually run whichever segment of code is activated within the case statement, and most of the addi- tional functions either are there as helper functions for other scripts or are used to set up the AI with such things as waypoints or enemy target transforms.

9.1 The AI State Control Script

Before getting to the main BaseAIController.cs script, take a look at AIStates.cs below. It contains all of the AIStates from the previous section stored in an enumerator list similar to the one used by the character controller from Chapter 5. This list will be used by the AI to determine its current required behavior. The list is in its own file (the AIStates.cs file) and in a namespace called AIStates so that we can easily make it accessible from any script used by the game:

using UnityEngine; namespace AIStates {

public enum AIState { moving_looking_for_target, chasing_target, backing_up_looking_for_target, stopped_turning_left, stopped_turning_right, paused_looking_for_target, translate_along_waypoint_path, paused_no_target, steer_to_waypoint, steer_to_target, } }