UE4.9:移动字符(未设置位置)

UE4.9: Move character (not set location)

本文关键字:设置 位置 字符 移动 UE4      更新时间:2023-10-16

我正在从我编写的外部应用程序发送命令。此外部应用程序正在通过UDP将键盘上按下的键发送到UE4。当UE4收到消息(按下的键,例如:"w")时,我想让角色向前走(只有1个字符)。

while (true)
{
    int32 bytes_read = 0;
    if (Socket->Recv(data, sizeof(data), bytes_read)){
        data[bytes_read] = 0;
       //Should I use this, instead of SetActorLocation?
        APlayerController* playerController = World->GetFirstPlayerController();
        ACharacter* myCharacter = UGameplayStatics::GetPlayerCharacter(World, 0);
        FVector characterLocation = myCharacter->GetActorLocation();
        FVector newCharacterLocation = characterLocation + FVector(10, 0, 0);
        //This just moves the character, but does not walk and no collision is included
        myCharacter->SetActorLocation(newCharacterLocation); 
        //FPlatformProcess::Sleep(0.1);
    }
}

注意:循环正在另一个线程上执行

SetActorLocation()在移动角色时对我不起作用,但本质上只是"传送"。我想指示角色移动,就像我在玩游戏一样,并以正常的方式控制角色。

我想通过我的外部应用程序来控制角色。我应该怎么做才能让角色移动,就像我自己在玩游戏一样,并按下,例如:"w",向前移动?

我想明白了。。。

  while (true)
    {
        int32 bytes_read = 0;
        if (Socket->Recv(data, sizeof(data), bytes_read)){
            data[bytes_read] = 0;
            ACharacter* myCharacter = UGameplayStatics::GetPlayerCharacter(World, 0);
            myCharacter->AddMovementInput(FVector(10, 0, 0), 5.0);
        }
    }