【问题标题】:Changing values in other parts of a class Unreal C++更改类 Unreal C++ 其他部分的值
【发布时间】:2017-08-07 10:13:35
【问题描述】:

基本上,我想增加一个对象的 Z 轴每个刻度。我可以在蓝图中做到这一点,但我似乎无法弄清楚如何从滴答声中访问类的开始播放部分并增加 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);

    // ...
}

【问题讨论】:

    标签: c++ unreal-engine4


    【解决方案1】:

    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)。这是更基于物理的方法,适用于更复杂的模拟。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-10
      • 1970-01-01
      • 1970-01-01
      • 2011-07-11
      • 2017-05-01
      • 2018-12-03
      相关资源
      最近更新 更多