Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Player Attacks | Player Behavior
Fighting Game in Unity
course content

Course Content

Fighting Game in Unity

Fighting Game in Unity

1. Unity Animation System
2. Player Behavior
3. Enemy Behavior
4. Improve the Game
5. Improve the Flappy Bird Game

Player Attacks

Gizmos

  • private void OnDrawGizmos(): This is a method declaration for the OnDrawGizmos callback, indicating that Unity will automatically call this method when Gizmos are drawn in the Scene view for visualization purposes;
  • Gizmos.DrawLine(...): This code draws a line using Gizmos. It takes two points as arguments to define the line;
  • transform.position: This represents the current position of the GameObject to which this script is attached;
  • ((Vector2)transform.position + 4f * Vector2.right * localScale.x): This calculates the second point of the line. It takes the current position of the GameObject (transform.position) and adds to it a vector that extends 4 units to the right (4f * Vector2.right). localScale.x is used to adjust the length of the line and the direction based on the scale of the GameObject in the x-axis direction.

Overall, this code draws a line in the Scene view from the current position of the GameObject to a point that is 4 units to the right, with the length and direction adjusted based on the GameObject's scale in the x-axis direction. We will use it to visualize each attack range of the player.

Attack Script

When we initiate the attack, we will call the StartAttacking function, which takes the range of the attack as a parameter. Since each attack has a different range, it will begin attacking and set up the range accordingly.

In the Update function, when the player is attacking, we will create a RayCast starting from the player, moving in the direction that the player is facing, with a distance equal to the range of the attack, and only filtering the enemyLayer.

If we hit an enemy, we will debug their name for now, but in the future, we will retrieve their health and attack them. Additionally, we have an EndAttacking function, which will conclude our attack.

Animation Events

Adding Events

In Unity's Animation window, you can add events to animation clips at specific frames. Right-click on the animation curve or keyframe to add an animation event and specify the function and optional parameters.

Defining Functions

Functions to be called by Animation Events must be public. Unity's Animation Event system supports optional float and string parameters, automatically invoking specified functions during animation playback to synchronize animations with gameplay logic and trigger various actions.

Everything was clear?

Section 2. Chapter 3
We're sorry to hear that something went wrong. What happened?
some-alt