Unleash Realistic Pursuit: AI Chasing the Player

Unleash Realistic Pursuit: AI Chasing the Player

Table of Contents

  1. Introduction
  2. Creating the Player
  3. Adding Detection Scripts
  4. Adding Transitions to the State Macro
  5. Creating the Chase State
  6. Copying Code from Wander State
  7. Getting the Player's Position
  8. Setting the Nav Agent Destination
  9. Removing Random Cooldown
  10. Adding an On Exit State
  11. Stopping the NPC's Pursuit
  12. testing the Chase State
  13. Conclusion

Creating the Chase State

In this video, we will be focusing on creating the chase state for our NPC. This state will allow the NPC to chase after the player when they are within sight. The chase state is similar to the wander state, but instead of moving to a random position, the NPC will move towards the player's position.

To begin, we will copy the logic from the wander state and paste it into the chase state. This includes the update event, which controls the movement of the NPC. We will also remove the onEnterState event and the random cooldown, as we want the NPC to start chasing the player immediately when they are detected.

Instead of generating a random position, we will retrieve the position of the player using a scene variable. This position will then be set as the destination for the nav agent, causing the NPC to move towards the player.

To ensure that the NPC stops pursuing the player when they leave the chase state, we will add an onExitState event. This event will set the nav agent's destination to its current location, effectively stopping its movement.

In conclusion, the chase state allows our NPC to actively pursue the player when they are within sight. This adds a realistic behavior to our Game and brings it one step closer to completion. Stay tuned for the next video where we will create the flow macro for the player to shoot at the Cursor's position.

Pros:

  • Adds realistic behavior to the game
  • Provides active pursuit of the player

Cons:

  • None

Highlights

  • The chase state allows the NPC to pursue the player.
  • The logic from the wander state is copied and modified for the chase state.
  • The nav agent's destination is set to the player's position to enable movement.
  • An onExitState event is added to stop the NPC's pursuit.
  • The chase state enhances the realism and immersion of the game.

FAQ

Q: Can I use this chase state for multiple NPCs in my game? A: Yes, you can reuse the chase state logic for multiple NPCs in your game, allowing them all to chase after the player when within range.

Q: What happens if the player moves out of the NPC's sight while being chased? A: If the player moves out of the NPC's sight while being chased, the NPC will continue to move towards the last known position of the player. However, if the player remains out of sight for a certain duration, the NPC will eventually stop pursuing.

Q: Can the chase state be modified to include additional behaviors, such as attacking the player? A: Yes, the chase state can be expanded to include additional behaviors, such as initiating an attack on the player once they are within range. This can be achieved by adding appropriate logic within the chase state or transitioning to a separate attack state.

Most people like

Find AI tools in Toolify

Join TOOLIFY to find the ai tools

Get started

Sign Up
App rating
4.9
AI Tools
20k+
Trusted Users
5000+
No complicated
No difficulty
Free forever
Browse More Content