首先构造物体
1 #include "FollowActor.h"
2
3 // Sets default values
4 AFollowActor::AFollowActor()
5 {
6 // Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
7 PrimaryActorTick.bCanEverTick = true;
8 myMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("myMesh"));
9
10 auto asset = ConstructorHelpers::FObjectFinder<UStaticMesh>(TEXT("StaticMesh'/Engine/VREditor/TransformGizmo/SM_Sequencer_Key.SM_Sequencer_Key'"));
11
12 if (asset.Succeeded()) {
13 myMesh->SetStaticMesh(asset.Object);
14 }
15 RootComponent = myMesh;
16
17 myMesh->SetSimulatePhysics(true);
18 }
在TICK中处理坐标位置信息。
1 void AFollowActor::Tick(float DeltaTime)
2 {
3 Super::Tick(DeltaTime);
4
5 auto pawn = GetWorld()->GetFirstPlayerController()->GetPawn();
6
7 FVector SelfLocation = GetActorLocation();
8 FVector pawnLocation = pawn->GetActorLocation();
9 FVector direction = pawnLocation - SelfLocation;
10 direction.Normalize();
11 direction *= 3;
12
13 SetActorLocation(SelfLocation + direction);
14 }
效果如下: