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数学库提供的方法
	}
}
相关推荐
z落落3 小时前
C# WinForm Socket 转串口设备服务器+Modbus CRC16校验+Socket 客户端
服务器·开发语言·c#
小灰灰搞电子9 小时前
Rust+Slint 实现动态消息提示框源码分享
开发语言·后端·rust
神仙别闹10 小时前
基于 C++ 实现两个有序链表序列的交集
java·c++·链表
传奇开心果编程11 小时前
【Rust入门知识点学与练】第21课:Trait 进阶 Advanced Traits
开发语言·学习·rust
新时代牛马11 小时前
字符设备驱动完整篇:从 cdev_add、file_operations 到chrdev_open 与排障
开发语言·python
swordbob11 小时前
ReentrantLock 与 AQS 完整学习手册
java·开发语言
白山编程大哥12 小时前
Java OutputStreamWriter 详解:从字符到字节的桥梁
java·开发语言·python
hansang_IR12 小时前
【题解】[APIO2023] 赛博乐园 / cyberland
c++·算法·图论
落羽的落羽12 小时前
【AI】快速理解AI应用的相关名词概念
linux·c++·人工智能·python·计算机网络·算法
刚入门的大一新生12 小时前
Linux-进程控制
linux·运维·服务器·c++