> For the complete documentation index, see [llms.txt](https://luisramirez.gitbook.io/unreal-action-combat/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://luisramirez.gitbook.io/unreal-action-combat/section-2-character-movement.md).

# Section 2: Character Movement

In this section, we took the time to set up the character's models animation with movement.

## Section Intro - Character Movement

This lecture does not contain notes.

## Importing the Player and Enemy Models

In this lecture, we imported assets from the Epic Games marketplace to our project. We added two assets called **Paragon: Greystone** and **Paragon: Grux**.&#x20;

To apply these models to our blueprints, we updated the **Mesh** component's **skeletal mesh** setting.&#x20;

<figure><img src="https://2690671547-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fmxj5y6CqbtMsDqrKngyI%2Fuploads%2FRAq3WclgWNn2V9MQI4FB%2Fimage.png?alt=media&amp;token=fe2b473d-5305-4c03-832e-d256e3cf00bc" alt=""><figcaption><p>Skeletal Mesh Asset Setting</p></figcaption></figure>

The assets we downloaded have various skins of the same model. So, you're more than welcome to use whatever skin you like.&#x20;

For the boss, we also had to adjust the position and rotation of the mesh. The mesh should  be inside the collision capsule and pointing in the same direction as the blue arrow, which is the facing direction of the blueprint. Other than that, you should be good to go with using the blueprints.

## Adding Animation Blueprints

In this lecture, we learned how to create an animation blueprint. Animation blueprints allow us to tell Unreal what animations are available on a character as well as control which animation to be playing. To create an animation blueprint, you must select a skeleton. It's essential to select the correct skeleton, otherwise the animations may not translate well on the actual blueprint responsible for rendering the character.

Once you create the animation blueprint, you must configure the blueprint with your character to use it. You can do so by setting the following settings:

* **Animation Mode -** Set this to **Use Animation Blueprint**
* **Animation Class** - The animation blueprint file.

## Adding Animation Blendspaces

In this lecture, we added animation blendspaces for the idle and jogging animations for both characters in our game. Animation blendspaces are a feature for smoothly transitioning between animations. If you want to learn how to create an animation blendspace, please watch the video.&#x20;

## Playing Animation Blendspaces

In this lecture, we learned how to add our animation blendspace to the animation blueprint, which is by simply dragging and dropping it. Next, we updated the labels of each axes and then hooked up a vertical axis.

<figure><img src="https://2690671547-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fmxj5y6CqbtMsDqrKngyI%2Fuploads%2FjykLTJeQPLvE6Lknhhs7%2Fimage.png?alt=media&amp;token=9728a0b7-23e8-4329-9d0b-7e7f82ed1261" alt=""><figcaption></figcaption></figure>

We're using a variable so that we can update it later. We did so from the event graph.

<figure><img src="https://2690671547-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fmxj5y6CqbtMsDqrKngyI%2Fuploads%2FO8tViyR8YUnbsxzsJFhp%2Fimage.png?alt=media&amp;token=5751a67b-b6b5-42e6-b7bf-64d96bf0327f" alt=""><figcaption></figcaption></figure>

We're using a few nodes here:

* **Event Blueprint Update Animation:** An event node that runs whenever the animation is updated. We're using it to update the variable frequently.
* **Try Get Pawn Owner:** Gets the current owner of this animation blueprint, which would be the player, so that we can grab their velocity.
* **Get Velocity:** Grabs the current velocity of the actor.
* **Vector Length:** Get's the vector's length as a float so that we can use it to update our variable.
* **Set:** Used to update the **CurrentVelocity** variable with the **Vector Length** node's value.

## Creating a C++ Anim Instance Class

In this lecture, we learned how to create a C++ class. Whenever you do so, one of the first things you should do is make sure it inherits from the proper class. Typically, the parent class can be found under the **Class Settings** section of a blueprint under an option called **Parent Class**. The full proper class name can be found by viewing the actual class itself.

<figure><img src="https://2690671547-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fmxj5y6CqbtMsDqrKngyI%2Fuploads%2FdNDhHeIrAkpgaeTqsdc1%2Fimage.png?alt=media&amp;token=11ebe0cc-7540-42e4-bfca-f765cdffcec1" alt=""><figcaption><p>Finding the current class of a blueprint.</p></figcaption></figure>

Once you find the proper class name, you can search for the class during creation. For this lecture, we created classes from two classes called `UAnimInstance` and `ACharacter`.

## Unreal Properties

In this lecture, we defined a variable for storing the current velocity inside the `UPlayerAnimInstance` class.

```cpp
UPROPERTY(BlueprintReadWrite, EditAnywhere)
float CurrentVelocity{ 0.0f };
```

A few things worth noting:

1. The variable is initialized with direct initialization over copy initialization to reduce the likelihood of unintended type conversion.
2. We're adding the `UPROPERTY` macro with the `BlueprintReadWrite` specifier to expose the variable through the blueprint editor.
3. The `EditAnywhere` specifier allows you to modify the variable through the details window.&#x20;

### Resources

* Properties - <https://dev.epicgames.com/documentation/en-us/unreal-engine/unreal-engine-uproperties>

## Converting Nodes Into Functions

In this lecture, we converted the nodes that we used for updating the velocity into its C++ equivalent. Here's the final result:

```cpp
void UPlayerAnimInstance::UpdateVelocity()
{
	APawn* PawnRef{ TryGetPawnOwner() };

	if (!IsValid(PawnRef)) { return; }

	FVector Velocity{ PawnRef->GetVelocity() };
	CurrentVelocity = static_cast<float>(Velocity.Length());
}
```

Here's what we did:

* `TryGetPawnOwner()` - Retrieves the actor that is using this animation blueprint.
* `IsValid()` - Checks if a variable holds a reference to an actor. Since the `TryGetPawnOwner()` function may not be able to find an actor, you should check the reference before doing anything else.
* `GetVelocity()` - Returns the actor's velocity.
* `Length()` - Converts the Vector's length into a float value.

It's important to note that the `FVector` type is an alias for the `TVector` type. Check out the resource section for a link on more info.

Lastly, we typecasted the return value of the `Length()` function to a `float` since it returns a `double` by using the `static_cast` function.

### Resources

* UAnimInstance Class - <https://dev.epicgames.com/documentation/en-us/unreal-engine/API/Runtime/Engine/Animation/UAnimInstance>&#x20;
* TVector - <https://dev.epicgames.com/documentation/en-us/unreal-engine/API/Runtime/Core/Math/TVector>

## Function Specifiers

In this lecture, we used function specifiers to expose our function to our blueprint. The `UFUNCTION()` macro can be used to do that. We must add the `BlueprintCallable` specifier like so:

```cpp
UFUNCTION(BlueprintCallable)
void UpdateVelocity();
```

<figure><img src="https://2690671547-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fmxj5y6CqbtMsDqrKngyI%2Fuploads%2FmCB2yXVoDdV6gqwg86PE%2Fimage.png?alt=media&amp;token=7783ff17-d1a6-4a8f-86f8-ed4f98188178" alt=""><figcaption></figcaption></figure>

When using this node, Unreal adds the target pin so that we can configure what function this object gets called on. By default, it'll call it on whatever class is using it.&#x20;

## Resources

* UFUNCTION Macro - <https://dev.epicgames.com/documentation/en-us/unreal-engine/ufunctions-in-unreal-engine>
