基于C++代码的UE4学习(三十二)——创建追向PAWN的物体

 

首先构造物体

 

 

 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 }
 

 

效果如下:

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值