11 #include "Components/PoseableMeshComponent.h" 12 #include "Components/PrimitiveComponent.h" 13 #include "Containers/Map.h" 14 #include "GameFramework/Character.h" 15 #include "GameFramework/CharacterMovementComponent.h" 16 #include "GameFramework/Pawn.h" 19 : Super(ObjectInitializer)
21 PrimaryActorTick.bCanEverTick =
true;
26 Super::OnPossess(InPawn);
28 auto *
Character = Cast<ACharacter>(InPawn);
31 UE_LOG(LogCarla, Error, TEXT(
"Walker is not a character!"));
35 auto *MovementComponent =
Character->GetCharacterMovement();
36 if (MovementComponent ==
nullptr)
38 UE_LOG(LogCarla, Error, TEXT(
"Walker missing character movement component!"));
43 MovementComponent->JumpZVelocity = 500.0f;
57 TArray<USkeletalMeshComponent *> SkeletalMeshes;
58 Character->GetComponents<USkeletalMeshComponent>(SkeletalMeshes,
false);
59 USkeletalMeshComponent *SkeletalMesh = SkeletalMeshes.IsValidIndex(0) ? SkeletalMeshes[0] :
nullptr;
60 if (!SkeletalMesh)
return;
63 auto *AnimInst = SkeletalMesh->GetAnimInstance();
64 if (!AnimInst)
return;
65 UWalkerAnim *WalkerAnim = Cast<UWalkerAnim>(AnimInst);
66 if (!WalkerAnim)
return;
69 FPoseSnapshot TempSnapshot;
70 SkeletalMesh->SnapshotPose(TempSnapshot);
75 for (
int i=0; i<TempSnapshot.BoneNames.Num(); ++i)
78 Transforms.
World = SkeletalMesh->GetSocketTransform(TempSnapshot.BoneNames[i], ERelativeTransformSpace::RTS_World);
79 Transforms.
Component = SkeletalMesh->GetSocketTransform(TempSnapshot.BoneNames[i], ERelativeTransformSpace::RTS_Actor);
80 Transforms.
Relative = SkeletalMesh->GetSocketTransform(TempSnapshot.BoneNames[i], ERelativeTransformSpace::RTS_ParentBoneSpace);
83 WalkerBones.
BoneTransforms.Add(TempSnapshot.BoneNames[i].ToString(), Transforms);
92 TArray<USkeletalMeshComponent *> SkeletalMeshes;
93 Character->GetComponents<USkeletalMeshComponent>(SkeletalMeshes,
false);
94 USkeletalMeshComponent *SkeletalMesh = SkeletalMeshes.IsValidIndex(0) ? SkeletalMeshes[0] :
nullptr;
95 if (!SkeletalMesh)
return;
98 auto *AnimInst = SkeletalMesh->GetAnimInstance();
99 if (!AnimInst)
return;
100 UWalkerAnim *WalkerAnim = Cast<UWalkerAnim>(AnimInst);
101 if (!WalkerAnim)
return;
104 if (WalkerAnim->
Snap.BoneNames.Num() == 0)
107 SkeletalMesh->SnapshotPose(WalkerAnim->
Snap);
110 TMap<FName, FTransform> BonesMap;
111 for (
const TPair<FString, FTransform> &pair : WalkerBones.
BoneTransforms)
113 FName BoneName = FName(*pair.Key);
114 BonesMap.Add(BoneName, pair.Value);
118 for (
int i=0; i<WalkerAnim->
Snap.BoneNames.Num(); ++i)
120 FTransform *Trans = BonesMap.Find(WalkerAnim->
Snap.BoneNames[i]);
123 WalkerAnim->
Snap.LocalTransforms[i] = *Trans;
133 TArray<USkeletalMeshComponent *> SkeletalMeshes;
134 Character->GetComponents<USkeletalMeshComponent>(SkeletalMeshes,
false);
135 USkeletalMeshComponent *SkeletalMesh = SkeletalMeshes.IsValidIndex(0) ? SkeletalMeshes[0] :
nullptr;
136 if (!SkeletalMesh)
return;
139 auto *AnimInst = SkeletalMesh->GetAnimInstance();
140 if (!AnimInst)
return;
141 UWalkerAnim *WalkerAnim = Cast<UWalkerAnim>(AnimInst);
142 if (!WalkerAnim)
return;
145 WalkerAnim->
Blend = Blend;
153 TArray<USkeletalMeshComponent *> SkeletalMeshes;
154 Character->GetComponents<USkeletalMeshComponent>(SkeletalMeshes,
false);
155 USkeletalMeshComponent *SkeletalMesh = SkeletalMeshes.IsValidIndex(0) ? SkeletalMeshes[0] :
nullptr;
156 if (!SkeletalMesh)
return;
159 auto *AnimInst = SkeletalMesh->GetAnimInstance();
160 if (!AnimInst)
return;
161 UWalkerAnim *WalkerAnim = Cast<UWalkerAnim>(AnimInst);
162 if (!WalkerAnim)
return;
165 SkeletalMesh->SnapshotPose(WalkerAnim->
Snap);
171 Super::Tick(DeltaSeconds);
void BlendPose(float Blend)
void ApplyWalkerControl(const FWalkerControl &InControl)
TMap< FString, FWalkerBoneControlOutData > BoneTransforms
void OnPossess(APawn *InPawn) override
float GetMaximumWalkSpeed() const
Maximum walk speed allowed in centimetres per second.
AWalkerController(const FObjectInitializer &ObjectInitializer)
void GetBonesTransform(FWalkerBoneControlOut &WalkerBones)
void Tick(float DeltaSeconds) override
TMap< FString, FTransform > BoneTransforms
void GetPoseFromAnimation()
void SetBonesTransform(const FWalkerBoneControlIn &WalkerBones)