【问题标题】:How to move an actor back and forth Unreal Engine如何来回移动演员 虚幻引擎
【发布时间】:2021-01-22 18:44:59
【问题描述】:

我是 Unreal Engine 的新手,我正在尝试将一个演员从他的原始放置位置移动到 B 点,然后返回到原始放置,然后循环移动到 B。我如何通过 C++ 做到这一点?我对这一切知之甚少,但我有一个关于研究它是如何工作的家庭作业。我知道如何制作 C++ Actor 类,但在 C++ 类中,如何将其分配给静态网格,以及如何使其在设置的 Actor 位置完成所有运动?

谢谢

【问题讨论】:

  • 另一种解决方案是添加 navmesh 和 AI,这将指向网格上方的点。有example on using it with blueprint,但肯定有一个只使用 Cpp 代码的地方。

标签: c++ unreal-engine4


【解决方案1】:

您将需要覆盖 Actor 的 Tick 函数,这使您能够在每一帧运行代码,然后您可以逐步对 Actor 位置向 TargetLocation 进行小幅调整。

这可以通过插值函数来实现。

FMath::VInterpConstantTo(const FVector& Current, const FVector& Target, float DeltaTime, float InterpSpeed);

使用此函数,您可以获得 CurrentLocation 和 TargetLocation 之间的增量 FVector,然后它将成为您的 Actors 的新位置。

一旦它到达其 TargetLocation,您就可以将此函数的 TargetLocation 值交换为 Actor 的原始位置,从而在其移动中创建一个乒乓动作。

【讨论】:

    【解决方案2】:

    好的,所以我通过执行以下操作解决了它:

    我在.h 文件中添加了以下内容

        APlateUp();
    
        UPROPERTY(EditAnywhere)
            UShapeComponent* Root;
    
        UPROPERTY(EditAnywhere)
            UStaticMeshComponent* MyMesh;
    
    float RunningTime;
    

    在公开课中

    然后在我添加的.cpp 文件中

        Root = CreateDefaultSubobject<UBoxComponent>(TEXT("Root"));
        RootComponent = Root;
    
        MyMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MyMesh"));
        MyMesh->AttachTo(RootComponent);
    

    在main函数中然后我添加了

        FVector NewLocation = GetActorLocation();
        float DeltaHeight = (FMath::Sin(RunningTime + DeltaTime) - FMath::Sin(RunningTime));
    
        NewLocation.Z += DeltaHeight * 200.0f;
        RunningTime += DeltaTime;
        SetActorLocation(NewLocation);
    

    当然你可以把NewLocation.Z改成X或者Y,你可以改变物体移动的力。

    这些解决方案是通过在 YouTube 上观看 Reuben Ward 提供的,因此有更多关于所有这些的解释。我只是在回答我的问题,所以如果有人遇到同样的问题,他们可以很容易地得到答案。另外,如果你想在某个位置停止对象,你可以这样做:

        if (NewLocation.Z < 7700.0) {
            float DeltaHeight = ((RunningTime + DeltaTime) - (RunningTime));
            NewLocation.Z += DeltaHeight * 100.0f;
            RunningTime += DeltaTime;
            SetActorLocation(NewLocation);
        }
    

    当然,将NewLocation.Z 更改为X 或Y,将7700.0 更改为您想要在该轴上的任何位置。这将检查当前的新位置是否小于您提供的值,如果是,它会继续在轴上移动对象。这是我自己的问题,所以如果您对此功能有任何疑问,请随时提出。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-28
      • 1970-01-01
      • 2022-06-19
      • 1970-01-01
      • 2021-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多