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数学库提供的方法
	}
}
相关推荐
mqiqe2 小时前
线程调度与 Schedulers:Project Reactor 并发模型的核心引擎
java·开发语言·网络
xixiaoyunya4 小时前
JavaScript 异步编程全解析:从回调地狱到 async/await 的演进之路
开发语言·javascript·ecmascript
精英的英4 小时前
记一次 Qt5 Language Server 开发
开发语言·vscode·qt
烧酒同学4 小时前
【C++】记录size of std::vector的巧妙坑
开发语言·c++·图形渲染
周周哈哈哈5 小时前
线程创建、执行、退出、回收
java·开发语言
gb42152875 小时前
python中Web应用服务器
开发语言·前端·python
萧瑟余晖5 小时前
Java深入解析篇三十六之分布式系统详解
java·开发语言·分布式
ZJU_统一阿萨姆6 小时前
【推理优化进阶】调度器的数学内核:排队论、SLO 与在线决策
开发语言·人工智能·语言模型·系统架构·vllm
平常心的技术小牛6 小时前
Qt-快速上手-QTextCursor
开发语言·qt
两个人的幸福online6 小时前
PHP 实现国密 SM3 踩坑记录:HMAC-SM3 签名对接 CNR 接口
开发语言·php