Objective: Alert the guards to the coin toss.
Carrying on from the last post. We will start by creating a SendAIToCoin method which takes in a Vector3 of coinPos. We can then pass the RaycastHit point when calling the method. Next we need to get all the guards so we use a GameObject array called guards and set it using FindGameObjectsWithTag.
We then need to run a foreach loop to iterate over the guards array. We need to add a public bool _coinTossed and a public Vector3 _coinPos in the guards so we can send this information from the Player.
Next back in the Players SendAIToCoin method, inside the foreach we need to get a handle on some of the guards components. We need the NavMeshAgent, GuardAI script and the Animator. Once we have these handles, we can set the guards coinTossed bool to true, pass the coin position, set the destination on the NavMeshAgent and set the guards walking animation to true.
Moving back to the guards script we need to update the logic for the guards doing their rounds. Currently, even sending all this information and the new set destination form the Player, the guards end up ignoring the calls because inside their Update method they instantly end up back moving to the current target they have set. We need to add an if coin is tossed into the current logic shown below.
In the above we need add an else statement after we add the logic for if coinTossed. In here we know the coin has been tossed and the guards are moving the the coins position. We need to check if the guards are within range of the coin then switch them back to idle.
Next we will trigger the Players animation to go with the coin toss. Open the animator and make a trigger parameter called Throw. Then make a transition from AnyState to Throw then make transitions between Throw and Idle and another between Throw and Walk. Select the transition between AnyState and Throw and set the condition in the Inspector to Throw.
Activate the Throw animation inside the Throw or right-click logic within the Player script inside the if Physics.Raycast statement where we instantiate the coin. We do this using SetTrigger instead of SetBool.
Finally we will add an audio queue trigger to tell the Player that if he throws a coin it will distract the guards, letting him get passed. For this we have a trigger point in the game scene. We will set the is trigger on the collider so we can use OnTriggerEnter to trigger the audio clip. We will also add a Rigidbody to the trigger object and turn off Use Gravity.
Next we create a script called VoiceOverTrigger and add the logic for the OnTriggerEnter using the Player tag. We will need to add a public AudioClip so we can assign the audio in the Inspector. We will play the audio using the Main.Camera postion for better audio.
In the next post, we will look at the Security Cameras.