ue4 相机移动到指定actor位置 SetViewTargetWithBlend

这篇博客介绍了如何在UE4中创建自定义C++类MyActor,并通过SetViewTargetWithBlend方法实现相机平滑移动到指定Actor的位置,详细讲解了MyActor的头文件和源代码实现,以及设置组件点击运行的步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1 新建c++类MyActor

2 MyActor.h

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "MyActor.generated.h"

UCLASS()
class ROTATE_API AMyActor : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	AMyActor();

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

	// declare variables
	UPROPERTY(EditAnywhere)
	AActor* MyActor2;
	
};

MyActor.cpp

// Fill out your copyright notice in the Description page of Project Settings.

#include "MyActor.h"
// include gameplay statics header file
#include "Kismet/GameplayStatics.h"

// Sets default values
AMyActor::AMyActor()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to
### 物体平滑移动指定位置 在 Unreal Engine 中,为了实现物体平滑地移动到特定的目标位置,通常会采用插值方法。这可以通过蓝图脚本或C++代码完成。 #### 蓝图解决方案 对于蓝图用户来说,可以利用`Move To`节点配合`Timeline`组件来创建一个简单的平滑运动效果。具体操作如下: - 创建一个新的 `Timeline` 组件,并设置关键帧以定义起始时间和结束时间。 - 添加两个浮点轨道分别对应 X 和 Y 坐标的位移变化。 - 设置好目标坐标作为终点,在事件图表里调用 `Play from Start` 方法启动这个 Timeline。 - 当到达设定的时间点时,更新 Actor位置属性[^1]。 然而更常见的方式是使用 `Add Movement Input` 或者直接修改 Transform 属性来进行线性的逐步逼近计算。 #### C++编程方式 如果倾向于编写原生代码,则可通过继承 AActor 类来自定义行为逻辑。下面给出一段示范如何让对象沿直线路径平稳过渡的例子: ```cpp // MyMovingObject.h #pragma once #include "CoreMinimal.h" #include "GameFramework/Actor.h" #include "MyMovingObject.generated.h" UCLASS() class MYPROJECT_API AMovingObject : public AActor { GENERATED_BODY() public: // Sets default values for this actor's properties AMovingObject(); protected: virtual void BeginPlay() override; private: UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="Movement") FVector TargetLocation; // 目标地点 float CurrentAlpha; UFUNCTION() void MoveToTarget(float DeltaTime); }; ``` ```cpp // MyMovingObject.cpp #include "MyMovingObject.h" AMovingObject::AMovingObject(){ PrimaryActorTick.bCanEverTick = true; } void AMovingObject::BeginPlay(){ Super::BeginPlay(); CurrentAlpha = 0.f; } void AMovingObject::Tick( float DeltaSeconds ){ Super::Tick(DeltaSeconds); MoveToTarget(DeltaSeconds); } void AMovingObject::MoveToTarget(float DeltaTime){ const float InterpSpeed = 5.0f * DeltaTime; CurrentAlpha += InterpSpeed; if (CurrentAlpha >= 1.0f) { SetActorLocation(TargetLocation); return; } FMath::VInterpConstantTo(GetActorLocation(), TargetLocation, DeltaTime, InterpSpeed); } ``` 这段程序展示了怎样基于时间增量不断调整当前实体的位置直到达到预期的目的地为止。这里采用了 `FMath::VInterpConstantTo()` 函数辅助处理向量之间的差值运算。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值