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数学库提供的方法
	}
}
相关推荐
jerryinwuhan4 小时前
基于各城市站点流量的复合功能比较
开发语言·php
迈巴赫车主5 小时前
Java基础:list、set、map一遍过
java·开发语言
智者知已应修善业5 小时前
【51单片机89C51及74LS273、74LS244组成】2022-5-28
c++·经验分享·笔记·算法·51单片机
南 阳6 小时前
Python从入门到精通day66
开发语言·python
十八旬7 小时前
快速安装ClaudeCode完整指南
开发语言·windows·python·claude
前进的李工7 小时前
EXPLAIN输出格式全解析:JSON、TREE与可视化
开发语言·数据库·mysql·性能优化·explain
Byron Loong8 小时前
【c++】为什么有了dll和.h,还需要包含lib
java·开发语言·c++
独隅8 小时前
CodeX + Visual Studio Code 联动的全面指南
开发语言·php
坚果派·白晓明8 小时前
【鸿蒙PC三方库移植适配框架解读系列】第一篇:Lycium C/C++ 三方库适配 — 概述与环境配置
c语言·开发语言·c++·harmonyos·开源鸿蒙·三方库·c/c++三方库
爱吃小白兔的猫9 小时前
LPA算法详解:一种近线性时间的图社区发现方法
开发语言·php