習(xí)5)
UE5學(xué)習(xí)51.ToCompactString() 向量—字符串//ToCompactString()void UMover_2::BeginPlay(){Super::BeginPlay(); FVector MyVector FVector(0,0,0);//MyVector繼承Vector其中有ToCompactString函數(shù)可以調(diào)用 FVector* P_MyVector MyVector; P_MyVector-X 100; P_MyVector-Y 100; P_MyVector-Z 100; UE_LOG(LogTemp, Warning, TEXT(MyVector is %s),*MyVector.ToCompactString());//和指針無(wú)關(guān)只是轉(zhuǎn)換成正確的格}2.Actor組件控制父類(lèi)Actor我們期望用組件控制Actor就可以在Mover的cpp中GetOwner函數(shù)獲取父類(lèi)Actor的地址之后解引用就可以調(diào)用該Actor中函數(shù)GetOwner()//返回一個(gè)指向該組件的Actor的指針通過(guò)組件mover讓父類(lèi)Actor移動(dòng)//.hFVector StartLocation; UPROPERTY(EditAnywhere) FVector MovingVelocity FVector(0.0f, 0.0f, 0.0f); UPROPERTY(EditAnywhere) float SetMovingDistance 0.0f;//.cppvoid UMover::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction){Super::TickComponent(DeltaTime, TickType, ThisTickFunction); FVector CurrentLocation GetOwner()-GetActorLocation(); float MovingDistance FVector::Dist(StartLocation, CurrentLocation); if (SetMovingDistance MovingDistance) { CurrentLocation CurrentLocation (DeltaTime * MovingVelocity); GetOwner()-SetActorLocation(CurrentLocation); }}要把父類(lèi)組件調(diào)成可移動(dòng)3.用數(shù)學(xué)函數(shù)實(shí)現(xiàn)移動(dòng)移動(dòng)代碼的改進(jìn)FMath::VInterpConstantTo需要頭文件#include “Math/UnrealMathUtility.h”用恒定的步長(zhǎng)從當(dāng)前向量插值到目標(biāo)向量//.h(部分)UPROPERTY(EditAnywhere,BlueprintReadOnly,CategoryMove) FVector MoveOffset;//某個(gè)軸移動(dòng)多少距離 UPROPERTY(EditAnywhere,BlueprintReadOnly,CategoryMove) float MoveTime 4.0f; UPROPERTY(EditAnywhere,BlueprintReadOnly,CategoryMove) bool ShouldMove false; FVector TargetLocation; FVector StartLocation;//cpp#include “Mover.h”#include “Math/UnrealMathUtility.h”void UMover_2::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction){Super::TickComponent(DeltaTime, TickType, ThisTickFunction); if (ShouldMove true) { FVector Current_Location GetOwner()-GetActorLocation(); float Speed MoveOffset.Length() / MoveTime;//MoveOffset.Length()以浮點(diǎn)數(shù)的形式返回這個(gè)向量的長(zhǎng)度 FVector New_Location FMath::VInterpConstantTo(Current_Location, TargetLocation, DeltaTime, Speed); GetOwner()-SetActorLocation(New_Location); } else { }}相比之前的移動(dòng)代碼更加好控制邏輯更加自洽4.實(shí)現(xiàn)踏板開(kāi)門(mén)效果shouldmove為真門(mén)開(kāi)shouldmove為假門(mén)關(guān)情況1為真目標(biāo)位置 起始位置 移動(dòng)偏移量情況2為假目標(biāo)位置 起始位置所以現(xiàn)在shouldmove控制的是移動(dòng)方向也就是是否開(kāi)關(guān)門(mén)所以我們可以搞個(gè)新的組件為Oppen_Close//基于mover組件進(jìn)行魔改//錯(cuò)誤寫(xiě)法void UOpen_Close::BeginPlay(){Super::BeginPlay(); StartLocation GetOwner()-GetActorLocation(); TargetLocation StartLocation MoveOFFset;//這行得寫(xiě)到if為真的情況下}// Called every framevoid UOpen_Close::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction){Super::TickComponent(DeltaTime, TickType, ThisTickFunction); if (shouldmove) { //如果這里重新定義TargetLocation那么我門(mén)剛開(kāi)始的時(shí)候設(shè)置為開(kāi)門(mén)那么門(mén)會(huì)開(kāi)。我們關(guān)門(mén)也會(huì)關(guān)但是當(dāng)我門(mén)再次把Open_Close設(shè)為真的時(shí)候門(mén)是不會(huì)開(kāi)的因?yàn)槲覀儧](méi)有重新設(shè)置TargetLocation所以用的還是else中的TargetLocation也就是StartLocation所以函數(shù)中傳的參數(shù)都是StartLocation就不會(huì)移動(dòng)了 } else { UE_LOG(LogTemp, Warning, TEXT(%d), shouldmove); TargetLocation StartLocation; } FVector CurrentLocation GetOwner()-GetActorLocation(); float speed MoveOFFset.Length() / MoveTime;//MoveOFFset.Length()返回浮點(diǎn)數(shù)移動(dòng)長(zhǎng)度/時(shí)間速度 FVector NewLocation FMath::VInterpConstantTo(CurrentLocation, TargetLocation, DeltaTime, speed); //FMath::VInterpConstantTo當(dāng)前位置目標(biāo)位置DeltaTime速度 GetOwner()-SetActorLocation(NewLocation);}//正確寫(xiě)法void UOpen_Close::BeginPlay(){Super::BeginPlay(); StartLocation GetOwner()-GetActorLocation();}void UOpen_Close::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction){Super::TickComponent(DeltaTime, TickType, ThisTickFunction); if (shouldmove) { //計(jì)算目標(biāo)位置 TargetLocation StartLocation MoveOFFset; } else { TargetLocation StartLocation; } FVector CurrentLocation GetOwner()-GetActorLocation(); float speed MoveOFFset.Length() / MoveTime; FVector NewLocation FMath::VInterpConstantTo(CurrentLocation, TargetLocation, DeltaTime, speed); GetOwner()-SetActorLocation(NewLocation);}但是上述代碼仍然有問(wèn)題即使我們到達(dá)了目標(biāo)位置但是我們還是會(huì)重新設(shè)置新的位置它嚴(yán)重影響性能。5.兩個(gè)向量比較 Equals()//.hUPROPERTY(VisibleAnywhere) bool ReachTarget false;//.cppif (shouldmove){ //計(jì)算目標(biāo)位置 TargetLocation StartLocation MoveOFFset; } else { TargetLocation StartLocation; } FVector CurrentLocation GetOwner()-GetActorLocation(); ReachTarget CurrentLocation.Equals(TargetLocation);//用于判斷兩個(gè)向量是否相等 if (ReachTarget true) { //到達(dá)目標(biāo)位置 GEngine-AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, TEXT(reach the target)); } else { //未到達(dá)目標(biāo)位置 float speed MoveOFFset.Length() / MoveTime; FVector NewLocation FMath::VInterpConstantTo(CurrentLocation, TargetLocation, DeltaTime, speed); GetOwner()-SetActorLocation(NewLocation); }psvoid UMover_2::BeginPlay(){Super::BeginPlay(); AActor* MyOwnerPtr GetOwner(); UE_LOG(LogTemp, Warning, TEXT(%s),*MyOwnerPtr-GetActorNameOrLabel()); UE_LOG(LogTemp, Warning, TEXT(%s),*MyOwnerPtr-GetName());}GetActorNameOrLabel()優(yōu)先返回你在場(chǎng)景大綱Outliner中手動(dòng)設(shè)置的、方便人類(lèi)閱讀的名稱(chēng)如 SM_CastleWallEntranceDoor。GetName()返回引擎底層自動(dòng)生成的對(duì)象標(biāo)識(shí)符通常帶有類(lèi)型后綴、隨機(jī)哈希碼或序列號(hào)如 StaticMeshActor_525 或 BP_TestingTargetDummy_C_UAID_2CF05D6D…