UE4创建一个可以移动的平台

使用UE4中的差值创建一个可以平滑移动的平台
在这里插入图片描述

  1. 创建一个C++命名:FloatingPlatform,自动生成的类:AFloatingPlatform
// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

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

UCLASS()
class FIRSTPROJECT_API AFloatingPlatform : public AActor {
	GENERATED_BODY()

public:
	// Sets default values for this actor's properties
	AFloatingPlatform();

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Platform")
	class UStaticMeshComponent* mess;

	UPROPERTY(EditAnywhere)
	FVector startPoint = FVector(0.0);

	UPROPERTY(EditAnywhere, meta = (MakeEditWidget = "true"))
	FVector endPoint = FVector(0.0);

	UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Platform")
	float interpSpeed = 2.0;

	FTimerHandle interpTimer;

	UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Platform")
	bool bIsInterp = false;

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

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

	void troggleInterp();
};
// Fill out your copyright notice in the Description page of Project Settings.


#include "FloatingPlatform.h"

// Sets default values
AFloatingPlatform::AFloatingPlatform() {
	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

	mess = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("mess"));
	RootComponent = mess;
}

// Called when the game starts or when spawned
void AFloatingPlatform::BeginPlay() {
	Super::BeginPlay();

	startPoint = GetActorLocation();
	endPoint += startPoint;

	GetWorldTimerManager().SetTimer(interpTimer, this, &AFloatingPlatform::troggleInterp, 1.0);

	distance = (endPoint - startPoint).Size();
}

// Called every frame
void AFloatingPlatform::Tick(float DeltaTime) {
	Super::Tick(DeltaTime);

	FVector currentLocation = GetActorLocation();
	FVector iterp = FMath::VInterpTo(currentLocation, endPoint, DeltaTime, interpSpeed);
	SetActorLocation(iterp);

	if ((currentLocation - endPoint).Size() < 0.1){
		FVector tempPoint = startPoint;
		startPoint = endPoint;
		endPoint = tempPoint;
	}
}

void AFloatingPlatform::troggleInterp() {
	bIsInterp = !bIsInterp;
}
  1. 创建对应的蓝图类,并且选中一个平台
    在这里插入图片描述
  2. 把平台拖动到场景中,运行即可
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wb175208

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值