Testing the PlayerController

Hit the Play button the test the scene. Clicking the left mouse button or A or X on the Touch controller will display a ray in the Scene window. Zombies that collide with the ray will die.

Everything is working well expect for two things:

  1. The zombies never attack the player
  2. Without visual feedback

it's almost impossible to tell if you've lined up your shot. So, let's address these last two things as we wrap up the project:

  1. Reopen the zombieController script.
  2. Inject the following functions at the end of the script. before the last curly bracket. to facilitate the zombie attack function:
private void OnCollisionEnter (Collision other) { 
// This code will initiate an attack when the player GameObject intersects
// with a zombie collider if (other.collider.tag == "Player" && !_isDead) { StartCoroutine(Attack
(other.collider.GameObject.GetComponent<PlayerController> ())); } } IEnumerator Attack (PlayerController player) { _isAttacking = true; _animator.SetBool ("Attack", true); yield return new WaitForSeconds (44f / 30f); Die (); }

In our game, a zombie attack occurs when the prefab's capsule collider begins touching the player's capsule collider. This interaction is determined by the OnCollisionEnter() which seeks to collect data using other.collider. In this case, we are checking that the other GameObject is tagged as Player.

Additionally, the if statement makes sure the zombie's _isDead attribute is false. If this were not the case then, once the zombie collided, it would continue to attack until killed, which is what would happen in the real world, but is not needed for our simulation.

Inside of the if statement is our final coroutine. Here, we are mimicking the coroutine relationship between Die() and DestroyThis() by having an Attack() function kicked off by the OnCollisionEnter() function. The only difference is that, in this case, we are also passing along player information.

The final function defines Attack(PlayerController player) which, as previously stated, is called as a coroutine when a prefab collides with the player GameObject. Here, we set the prefab's _isAttacking attribute to true along with the Animator's Attack parameter. The WaitForSeconds statement is used to make sure the attack animation is completed before the final statement destroys the prefab.

The last step before closing the script is to make sure all curly brackets, { }, are properly closed.

  1. Save the script, return to Unity and play the scene.

At this point, the zombies spawn and move toward the player. They die when we shoot them or after they attack. Unfortunately, it is difficult to determine exactly where our shots will land. We will fix this by adding a targeting graphic to the player's view.

  1. Stop the game before adding a targeting sprite to the Player GameObject.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.40.43