在主UI蓝图中设置调用设置时间节点设置时间
Now:返回本地时间与日期
新建一个float变量用来控制偏移速度,新建一个bool来跳出偏移的结束与开始,当bool为假的时候我们开始偏移float变量加加
,偏移大于一个值就将bool设置为真,也就是把这个数值返回给bool也算是偷个懒,因为这个值必定为真,这个值也是一个开始回滚的阀值,bool为真后就进行向上回滚也就是那个阀值减减
,一直减到那个阀值为0了给bool变量就会是0了,bool变量就会为假,又开始回滚向下了#include
:这是虚幻自带的会提供一些文件的函数,这个是添加在.h
文件中,.cpp
文件中也加这个头文件.cpp
中还要添加头文件
#include
:提供一些基础的文件操作#include
:用于管理不同平台的文件夹一些操作#include "Containers/UnrealString.h"
:处理虚幻的字符串的一些操作// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include
#include "MyBlueprintFunctionLibrary.generated.h"
/**
*
*/
UCLASS()
class DIGITALTWIN_OBEJCT_API UMyBlueprintFunctionLibrary : public UBlueprintFunctionLibrary
{
GENERATED_BODY()
public:
//读取Text文件
UFUNCTION(BlueprintCallable)
static FString ReadText(FString FileName);
//写入Text文件
UFUNCTION(BlueprintCallable)
static bool WriteText(FString Content, FString Path);
};
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyBlueprintFunctionLibrary.h"
#include
#include
#include
#include "Containers/UnrealString.h"
FString UMyBlueprintFunctionLibrary::ReadText(FString FileName)
{
FString ResultString;
//读取文本文件
FFileHelper::LoadFileToString(ResultString, *(FileName));
return ResultString;
}
bool UMyBlueprintFunctionLibrary::WriteText(FString Content, FString Path)
{
bool Result;
//写入内容
Result = FFileHelper::SaveStringToFile(Content, *(Path));
return Result;
}
meta = MakeEditWidger = true
属性,添加一个速度变量,因为要使用定时器,添加延迟时间,时间句柄以及bool变量是否开启插值计算和一个表示距离的变量 UPROPERTY(VisibleAnywhere, Category = "Properties")
class UStaticMeshComponent* MyMesh;
UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Car")
FVector StartPoint{};
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Car", meta = (MakeEditWidget = "true"))
FVector EndPoint {};
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Car")
float Speed {};
UPROPERTY(EditAnywhere, Category = "Car")
float DelayTime;
FTimerHandle InterpTimerHandle;
bool bInterping;
float Distance;
注意meta = MakeEditWidger = true
属性是与Actor自身的相对位置,也就是说在场景中小车所在这个位置与属性所在那个位置是相对Actor来言的,并不是世界位置
实际上属性的位置在这里,也就是属性位置加上原本Actor位置,不要被表明的属性那个标位所误导
使小车来回移动逻辑:初始化速度与来回时间与是否插值一开始为真,然后获取Actor起点与终点世界位置
Speed = 2.f;
DelayTime = 2.0f;
bInterping = true;
// Called when the game starts or when spawned
void AMyActorOne::BeginPlay()
{
Super::BeginPlay();
//获取Actor的世界位置
StartPoint = GetActorLocation();
Distance = EndPoint.Size();
EndPoint += StartPoint;
}
// Called every frame
void AMyActorOne::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
FVector CurrentLocation = GetActorLocation();
FVector NewLocation = FMath::VInterpConstantTo(CurrentLocation, EndPoint, DeltaTime, Speed);
SetActorLocation(NewLocation);//移动到新位置
float NewDistance = (GetActorLocation() - StartPoint).Size();
if (Distance - NewDistance <= 0.5f)
{
bInterping = !bInterping;
//Lambda表达式
auto InterpState = [this]()
{
bInterping = !bInterping;
};
//开启定时器
GetWorldTimerManager().SetTimer(InterpTimerHandle, FTimerDelegate::CreateLambda(InterpState), DelayTime, false);
//交换起点与终点
FVector temp = StartPoint;
StartPoint = EndPoint;
EndPoint = temp;
}
}
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "MyActorOne.generated.h"
UCLASS()
class DIGITALTWIN_OBEJCT_API AMyActorOne : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AMyActorOne();
UPROPERTY(VisibleAnywhere, Category = "Properties")
class UStaticMeshComponent* MyMesh;
UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Car")
FVector StartPoint{};
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Car", meta = (MakeEditWidget = "true"))
FVector EndPoint {};
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Car")
float Speed {};
UPROPERTY(EditAnywhere, Category = "Car")
float DelayTime;
FTimerHandle InterpTimerHandle;
bool bInterping;
float Distance;
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
};
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyActorOne.h"
#include "UObject/ConstructorHelpers.h"
#include "Components/StaticMeshComponent.h"
#include "TimerManager.h"
// Sets default values
AMyActorOne::AMyActorOne()
{
// 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;
MyMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MyMesh"));
RootComponent = MyMesh;
ConstructorHelpers::FObjectFinder<UStaticMesh> StaticMeshAsset(TEXT("/Script/Engine.StaticMesh'/Engine/EngineMeshes/Cube.Cube'"));
ConstructorHelpers::FObjectFinder<UMaterialInterface> MaterialAsset(TEXT("/Script/Engine.Material'/Engine/BasicShapes/BasicShapeMaterial.BasicShapeMaterial'"));
if (StaticMeshAsset.Succeeded() && MaterialAsset.Succeeded())
{
MyMesh->SetStaticMesh(StaticMeshAsset.Object);
MyMesh->SetMaterial(0, MaterialAsset.Object);
}
Speed = 2.f;
DelayTime = 2.0f;
bInterping = true;
}
// Called when the game starts or when spawned
void AMyActorOne::BeginPlay()
{
Super::BeginPlay();
//获取Actor的世界位置
StartPoint = GetActorLocation();
Distance = EndPoint.Size();
EndPoint += StartPoint;
}
// Called every frame
void AMyActorOne::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
FVector CurrentLocation = GetActorLocation();
FVector NewLocation = FMath::VInterpConstantTo(CurrentLocation, EndPoint, DeltaTime, Speed);
SetActorLocation(NewLocation);//移动到新位置
float NewDistance = (GetActorLocation() - StartPoint).Size();
if (Distance - NewDistance <= 0.5f)
{
bInterping = !bInterping;
//Lambda表达式
auto InterpState = [this]()
{
bInterping = !bInterping;
};
//开启定时器
GetWorldTimerManager().SetTimer(InterpTimerHandle, FTimerDelegate::CreateLambda(InterpState), DelayTime, false);
//交换起点与终点
FVector temp = StartPoint;
StartPoint = EndPoint;
EndPoint = temp;
}
}
UPROPERTY(EditAnywhere, Category = "Car")
float RotationRate;
int a;//标识位
// Called every frame
void AMyActorTwo::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
FRotator CurrentRotation = GetActorRotation();
if (CurrentRotation.Yaw > 90.f)
{
a = 1;
}
else if (CurrentRotation.Yaw < -90.f)
{
a = 2;
}
if (a == 1)
{
CurrentRotation = FRotator(CurrentRotation.Pitch, CurrentRotation.Yaw - RotationRate * DeltaTime, CurrentRotation.Roll);
SetActorRotation(CurrentRotation);
}
if (a == 2)
{
CurrentRotation = FRotator(CurrentRotation.Pitch, CurrentRotation.Yaw + RotationRate * DeltaTime, CurrentRotation.Roll);
SetActorRotation(CurrentRotation);
}
}
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "MyActorTwo.generated.h"
UCLASS()
class DIGITALTWIN_OBEJCT_API AMyActorTwo : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AMyActorTwo();
UPROPERTY(VisibleAnywhere, Category = "Properties")
class UStaticMeshComponent* MyMesh;
UPROPERTY(EditAnywhere, Category = "Car")
float RotationRate;
int a;//标识位
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
};
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyActorTwo.h"
#include "UObject/ConstructorHelpers.h"
#include "Components/StaticMeshComponent.h"
// Sets default values
AMyActorTwo::AMyActorTwo()
{
// 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;
MyMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MyMesh"));
RootComponent = MyMesh;
ConstructorHelpers::FObjectFinder<UStaticMesh> StaticMeshAsset(TEXT("/Script/Engine.StaticMesh'/Engine/EngineMeshes/Cube.Cube'"));
ConstructorHelpers::FObjectFinder<UMaterialInterface> MaterialAsset(TEXT("/Script/Engine.Material'/Engine/BasicShapes/BasicShapeMaterial.BasicShapeMaterial'"));
if (StaticMeshAsset.Succeeded() && MaterialAsset.Succeeded())
{
MyMesh->SetStaticMesh(StaticMeshAsset.Object);
MyMesh->SetMaterial(0, MaterialAsset.Object);
}
RotationRate = 10.f;
}
// Called when the game starts or when spawned
void AMyActorTwo::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AMyActorTwo::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
FRotator CurrentRotation = GetActorRotation();
if (CurrentRotation.Yaw > 90.f)
{
a = 1;
}
else if (CurrentRotation.Yaw < -90.f)
{
a = 2;
}
if (a == 1)
{
CurrentRotation = FRotator(CurrentRotation.Pitch, CurrentRotation.Yaw - RotationRate * DeltaTime, CurrentRotation.Roll);
SetActorRotation(CurrentRotation);
}
if (a == 2)
{
CurrentRotation = FRotator(CurrentRotation.Pitch, CurrentRotation.Yaw + RotationRate * DeltaTime, CurrentRotation.Roll);
SetActorRotation(CurrentRotation);
}
}