UE5 C++ 重要点

1.控制相关
springArm->bUsePawnControlRotation=true;
camera->bUsePawnControlRotation=false;


2.动画相关
做动画在蓝图里用TimeLine


3.延迟相关
C++里的延迟用FTimerHandle
.h里定义
FTimerHandle closeHandle;
.cpp 里使用
GetWorldTimerManager().SetTimer(closeHandle,this,&AStageDoor::DelayCloseDoor,2.0f);

GetWorldTimerManager().ClearTimer(closeHandle);//立即停止不去执行回调


4.生成Actor
.h
TSubclassOf<YourClass> BlueprintVar;

.cpp
UWorld* const World = GetWorld(); // get a reference to the world  
if (World)   
{  
   // if world exists  
   YourClass* YC = World->SpawnActor<YourClass>(BlueprintVar, SpawnLocation, SpawnRotation);  


5.可以显示在editor中的点
UPROPORTY(EditAnywhere,meta=(MakeEditWidget=true))
FVector EndPoint;

6.粒子特效
组件用
UParticleSysyemComponent*
需要生成的用
UParticleSystem*

生成方式:
UGameplayStatics::SpawnEmitterAtLocation(GetWorld(),UParticleSystem*,Location,Rotation,IsAutoDelete);

7.播放音效
音效右键创建 Sound Cue
代码播放:
USoundCue*

UGameplayStatics::PlaySound2D(this,USoundCue*,Volume)

8.类的继承相关,碰撞继承
父类:
virtual ClassName();
子类:
virtual ClassName() override;
子类有自己的构造函数,里面需不要调取父类的构造函数
子类的ClassName()需要调取父类的:
Super::ClassName();


9.想要HUD的UI里使用c++代码
在 项目名.Build.cs 里加入"UMG"

10.要想C++代码现实UI需要先创建个
PlayerController类
需要自己写BeginPlay();
之后在PlayerController类里添加下面的UI代码,创建BP
之后在世界场景设置里选择该BP


11.UI代码
//在Eidtor用引用UMG资源
UPROPERTY(...)
TSubclssOf<class UUserWidget> HUDOverlayAsset;
//之后代码使用
UPROPERTY(...)
UUserWidget* HUDOverlay;

if(HUDOverlayAsset;)
{
           HUDOverlay= CreateWidget<UUserWidget>(this,HUDOverlayAsset);
    HUDOverlay->AddToViewport();
    HUDOverlay->SetVisibility(ESlateVisibility::Visible);
}


12.碰撞检测里的OtherActor可以转化成需要的类,类似GetComponent
类名* aaa=Cast<类名>(OtherActor);
if(aaa)
{
    aaa->....
}


12.UE4中的枚举
UENUM(BlueprintType)
enum class EMovementStatus : uint8
{
    EMS_Normal UMETA(DisplayName = "Normal"),
    EMS_Sprinting UMETA(DisplayName = "Spriting"),

    EMS_Max UMETA(DisplayName = "Default Max")
}
枚举类型前面一定要加E否则会报错

UPROPERTY(...)
 EMovementStatus  MovementStatus;


13.现实debug球
UKismetSystemLibrary::DrawDebugSphere(this,Location,Radius,片段数量,FColor,显示时间,厚度)


14.TArray<FVector> positions;

for(auto pos : positions)
{
}

15.想用AAIController
必须要在build.cs里加
"AIModule"

16.局部骨骼碰撞
CombatCollision =CreateDefaultSubobject<UBoxComponent>(TEXT("CombatCollision"));
CombatCollision->AttachToComponent(GetMesh(),FAttachmentTransformforRules::SnapToTargetIncludingScale,FName("EnemySocket"));

需要在骨骼处右键添加骨骼插槽
用AttachToComponent而不是SetupAttachment

17.插值转向目标,写在Tick里
FVector Target=CombatTarget->GetActorLocation();
FRotator LookAtRotation=UKismetMathLibrary::FindLookAtRotation(GetActorLocation(),Target);
FRotator LookAtRotationYaw = FRotator(0.0f,LookAtRotation.Yaw,0.f);
FRotator InterpRotation =FMath::RInterpTo(GetActorRotation(),LookAtRotationYaw,DeltaTime,InterpSpeed);
SetActorRotation(InterpRotation);

18.AActor下面有用来接收伤害的函数,不需要调用Super
virtual float TakeDamage(float DamageAmount,struct FDamageEvent const & DamageEvent,class AController * EventInstigator,AActor * DamageCauser)  override  的重载函数
施加伤害的全局函数
UGameplayStatics::ApplyDamage(AActor * DamagedActor,float BaseDamage,AController * EventInstigator,AActor * DamageCauser, TSubclassOf< class UDamageType > DamageTypeClass)

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值