UE5 C++ 使C++创建动画蓝图

文章目录

警告:

动画蓝图不需要运行游戏也会一直执行,因此在编写代码时非常容易引发空指针崩溃

因此编写动画蓝图时需要关闭UE,或严格执行空指针检查

创建动画蓝图

继承AnimInstance

常用属性如下

头文件

csharp 复制代码
// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "Animation/AnimInstance.h"
#include "SlashAnimInstance.generated.h"

class ASlashCharacter;
/**
 * 
 */
UCLASS()
class CPPLEARN_API USlashAnimInstance : public UAnimInstance
{
	GENERATED_BODY()
	
public:
	virtual  void NativeInitializeAnimation() override;
	virtual  void NativeUpdateAnimation(float DeltaSeconds) override;
	
	UPROPERTY(BlueprintReadOnly)
	ASlashCharacter* SlashCharacter; //使用这个动画蓝图的角色
	UPROPERTY(BlueprintReadOnly,Category=Movement)
	class UCharacterMovementComponent* SlashCharacterMovement; //角色的运动组件
	UPROPERTY(BlueprintReadOnly,Category=Movement)
	float GroundSpeed;//地面速度
};

源文件

cpp 复制代码
// Fill out your copyright notice in the Description page of Project Settings.


#include "Character/SlashAnimInstance.h"
#include "Character/SlashCharacter.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "Kismet/KismetMathLibrary.h"

void USlashAnimInstance::NativeInitializeAnimation()
{
	Super::NativeInitializeAnimation();
	SlashCharacter = Cast<ASlashCharacter>(TryGetPawnOwner());//获取角色 TryGetPawnOwner获取到的是pawn,需要强转成character
	if (SlashCharacter)
	{
		 SlashCharacterMovement = SlashCharacter->GetCharacterMovement();//获取角色的运动组件
	}
}

void USlashAnimInstance::NativeUpdateAnimation(float DeltaSeconds)
{
	Super::NativeUpdateAnimation(DeltaSeconds);
	if (SlashCharacter)
	{	
		GroundSpeed = UKismetMathLibrary::VSizeXY(	SlashCharacterMovement->Velocity);//获取地面速度,只考虑xy方向,使用kismet数学库提供的方法
	}
}
相关推荐
郝学胜_神的一滴21 小时前
CMake 30:循环语法全解|foreach_while双循环精讲、迭代技巧与实战避坑指南
c++·cmake
卷无止境3 天前
C++ 的Eigen 库全解析
c++
卷无止境3 天前
现代 C++特性大盘点:一门脱胎换骨的老语言
c++·后端
郝学胜_神的一滴3 天前
CMake 27:缓存变量的特性、语法、类型与实操全解
c++·cmake
博客18005 天前
酷宝的使用方法,超好用的免费界面库,C++、MFC可用
c++·mfc·界面库·库来帮·酷宝
郝学胜_神的一滴5 天前
CMake 026:属性体系精讲、四大作用域全解 & 实战代码落地
c++·cmake
众少成多积小致巨6 天前
JNI (Java Native Interface) 技术手册中文参考指南
android·java·c++
clint45610 天前
C++进阶(1)——前景提要
c++
夜悊10 天前
C++代码示例:进制数简单生成工具
c++
郝学胜_神的一滴10 天前
CMake 021: IF 条件判据详诠
c++·cmake