gpt4 book ai didi

c++ - 更改类的其他部分的值 Unreal C++

转载 作者:行者123 更新时间:2023-11-30 05:10:41 27 4
gpt4 key购买 nike

基本上,我想在对象的每个刻度上增加 Z 轴。我可以在 Blueprint 中做到这一点,但我似乎无法弄清楚如何从滴答和增量 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);

// ...
}

最佳答案

UMoveUp:BeginPlay()中,您可以设置初始值。它只在 actor 生成时被调用一次。你想使用 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 )。这是一种更基于物理学的方法,适用于更复杂的模拟。

关于c++ - 更改类的其他部分的值 Unreal C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45544493/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com