Book Image

Unity 2020 By Example - Third Edition

By : Robert Wells
Book Image

Unity 2020 By Example - Third Edition

By: Robert Wells

Overview of this book

The Unity game engine, used by millions of developers around the world, is popular thanks to its features that enable you to create games and 3D apps for desktop and mobile platforms in no time. With Unity 2020, this state-of-the-art game engine introduces enhancements in Unity tooling, editor, and workflow, among many other additions. The third edition of this Unity book is updated to the new features in Unity 2020 and modern game development practices. Once you’ve quickly got to grips with the fundamentals of Unity game development, you’ll create a collection, a twin-stick shooter, and a 2D adventure game. You’ll then explore advanced topics such as machine learning, virtual reality, and augmented reality by building complete projects using the latest game tool kit. As you implement concepts in practice, this book will ensure that you come away with a clear understanding of Unity game development. By the end of the book, you'll have a firm foundation in Unity development using C#, which can be applied to other engines and programming languages. You'll also be able to create several real-world projects to add to your professional game development portfolio.
Table of Contents (16 chapters)

Attacking the player

The third and final state for the NPC is the attack state, during which the chick will periodically attack the player (or at least pretend to attack the player, as you'll see shortly).

This state can only be reached from the chase state. During a chase, the chick will check whether they are within attacking distance. If so, the chase state will request that the FSM change from chasing to attacking. If, during an attack, the player retreats, then the chick will change from attacking to chasing.

The attack state will have a related animation, as we alluded to in Chapter 7, Creating Artificial Intelligence, when we created the animations for the chick; however, before we create the custom animations, we'll write the state's implementation.

Implementing the state

As with the previous two states, the attack state is contained in a separate class:

  1. Create an AttackState class:
    public class AttackState : MonoBehaviour, IFSMState
    {
     ...