Creating Evasive AI for a Frog: A Step-by-Step Guide

Creating Evasive AI for a Frog: A Step-by-Step Guide

Table of Contents

  1. Introduction
  2. Setting up the State Machine
  3. Resting State
  4. Evading State
  5. Adding Danger Detection
  6. Implementing Jumping Animation
  7. Creating a Zigzag Movement
  8. Randomizing Jump Direction
  9. testing and Tweaking
  10. Conclusion

🐸 Creating Simple AI for a Creature: A Frog's Adventure

In this Tutorial, we will explore how to use state machines to create a simple AI for a creature, specifically a frog. Developer DS2's question on the Discord Channel inspired this tutorial – how to make a frog that moves away from the player. By implementing this AI behavior, you can bring your Game world to life and add realism to various projects. So, let's dive into the exciting tricks that will help us create an evasive frog AI.

1. Introduction

Before we begin, let's make sure we have all the necessary assets and repositories set up. You can find the Package containing the player controller and frog assets linked below. In this tutorial, we'll be using free assets from the Unity Asset Store, with a few tweaks to enhance their functionality. So, it is recommended to use the assets provided to follow along seamlessly.

Repository: Link to the package

2. Setting up the State Machine

To start, we need to create a state machine for our frog AI. Open the Bolt visual scripting tool and add a state machine to the frog GameObject. For this tutorial, we will create two states: "Resting" and "Evading" (or "Running Away"). Creating a new state is simple – right-click on the canvas, select "Create Flow State," and name it accordingly. Feel free to rename your state machine to "Creature ai Tutorial" or any other suitable name.

3. Resting State

Let's focus on the resting state first, where the frog remains calm and idle. To achieve this, we'll utilize the animator component on the frog and set the "Idle" trigger parameter. Create an "On Enter" unit within the resting state, and add an "Animator Set Trigger" unit with the trigger name set to "Idle." Make sure the trigger name matches the parameter name in your animator controller.

💡 Tip: Reducing the frequency of update calls will improve performance. We can accomplish this by implementing a timer that triggers the animations every 0.5 seconds. To do this, use a "Timer Started" unit and set the duration to 0.5 seconds. Inside the event, add a "Check Danger" custom event to ensure the frog periodically checks for danger.

4. Evading State

In the evading state, our frog will exhibit a behavior to escape from danger. When the frog senses danger, it will immediately turn away from the player. To achieve this, use the "Quaternion Look Rotation" unit to get the player's forward direction. Set the frog's rotation to the player's forward rotation using the "Set Rotation" unit. Additionally, set the "Jump" trigger parameter in the animator component to trigger the jumping animation.

5. Adding Danger Detection

To implement danger detection, we need to create a boolean variable named "IsEndangered" in the frog's inspector. Set it as an object variable and make it visible in the inspector. Additionally, create a custom event named "Check Danger" in the transition from the resting state to the evading state. In the graph, use a "Branch" unit connected to the "IsEndangered" variable. If the variable is true, transition from resting to evading. Repeat the process for the evading to resting transition, but this time check for when the "IsEndangered" variable is false.

6. Implementing Jumping Animation

Let's make the frog's evasion more dynamic by adding jumping animations. In the evading state, create an "On Enter" unit. Use the "Quaternion Look Rotation" unit again to get the player's forward direction. Set a "Slurp" unit to rotate the frog away from the player's direction gradually. Set the rotation of the frog using the "Set Rotation" unit. Finally, trigger the jump animation using the "Animator Set Trigger" unit.

7. Creating a Zigzag Movement

To make the frog's evasion more interesting, let's make it hop in a zigzag pattern. To achieve this, we'll use quaternion euler units to alternate between different rotations. Multiplying the rotations with quaternions allows us to add or subtract angles. By multiplying the player's forward direction by -1, we can flip the frog's rotation. Implement this logic to create a zigzag movement pattern in the evading state.

8. Randomizing Jump Direction

Adding more variety to the frog's evasion, we can randomize its jump direction. Three out of four times, the frog will jump away from the player. However, one out of four times, the frog will completely reverse its direction. To implement this, generate a random integer using the "Random Range" unit. Use the "Switch on Integer" unit to determine the jump direction based on the random number. This unexpected reversal will make the frog's evasion behavior more unpredictable and exciting.

9. Testing and Tweaking

Now that we have implemented the frog's evasive behavior, it's time to test and tweak the AI. Run the game and observe how the frog moves away from the player, jumps, and adjusts its rotation. Play around with the variables and parameters to achieve your desired behavior. Feel free to experiment and make adjustments to enhance the frog's AI and make it more suited to your game.

10. Conclusion

Congratulations! You have successfully created a simple AI for a frog using state machines. By following this tutorial, you have learned how to make a creature evade the player, adding life and immersion to your game world. The concepts and techniques introduced here can be applied to various projects to create engaging AI behaviors. Remember to experiment and explore new possibilities to enhance your game development skills.


Highlights:

  • Implementing simple AI for a frog
  • Using State Machines in Unity
  • Resting state and Evading state behavior
  • Danger detection and response
  • Jumping animation and randomization
  • Creating zigzag movement for evasion
  • Testing, tweaking, and enhancing AI behavior

FAQ:

Q: Can I use this AI system for other types of creatures? A: Absolutely! The concepts and techniques covered here can be applied to different creatures and AI behaviors in your games. Feel free to adapt and modify them as needed.

Q: Can I adjust the frequency and distance at which the frog detects danger? A: Yes, you can modify the parameters to suit your game's requirements. Experiment with different values to achieve the desired gameplay experience.

Q: How can I make the frog's evasion behavior more complex? A: Through further experimentation and combining different AI techniques, you can create more intricate evasion behaviors. Consider adding obstacle avoidance, pathfinding, or group dynamics to make the AI more advanced.

Q: Is this AI system performance-friendly? A: Yes, by utilizing timers and minimizing the use of update calls, this AI system is optimized for performance. However, for more complex AI behaviors, additional optimization may be required.


Resources:

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