基本上,我想增加一个对象的每一个Z轴。我可以在Blueprint中做到这一点没有问题,但我似乎无法弄清楚如何从tick中增加类的begin play部分并增加ActorLocation。
// Fill out your copyright notice in the Description page of Project Settings.
#include "MoveUp.h"
#include "GameFramework/Actor.h"
// Sets default values for this component's properties
UMoveUp::UMoveUp()
{
// Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features
// off to improve performance if you don't need them.
PrimaryComponentTick.bCanEverTick = true;
// ...
}
// Called when the game starts
void UMoveUp::BeginPlay()
{
Super::BeginPlay();
AActor* Owner = GetOwner();
FVector ActorLocation = FVector(0.f,0.f,200.f);
Owner->SetActorLocation(ActorLocation, false) ;
}
// Called every frame
void UMoveUp::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
// ...
}
答案 0 :(得分:1)
在UMoveUp:BeginPlay()
中,您可以设置初始值。当演员被催生时,它只被调用一次。您想使用TickComponent
:
// Called every frame
void UMoveUp::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
// this could be "cached" as private property - better performance
auto owner = GetOwner();
// now we need to actually move by Delta * base value
// note that we are ignoring actor's rotation
auto newActorLocation = owner->GetActorLocation() + FVector(0.f,0.f,200.f) * DeltaTime;
owner->SetActorLocation(newActorLocation, false);
}
使用DeltaTime
非常重要,因为您的 FPS 值可以(并且会)随时间变化,并且还取决于游戏(视觉)设置和用户的PC。无论帧率如何,使用DeltaTime
都可以确保相同的体验。
另一种可能的方法是使用UPrimitiveComponent::AddForce(...)
(UE Docs)。这是一种更基于物理的方法,它适用于更复杂的模拟。