如何在虚幻引擎 5 中使用 c++ 执行一个角色的函数(在场景组件中被定义,它是演员类的一部分)

问题描述 投票:0回答:0

基本上,我想用 C++ 在虚幻引擎 5 中打开一扇门。我从 Sketchfab 下载了一个门网格,但枢轴点在网格的发送者处,所以我创建了一个演员类并将场景组件附加到它,然后我附加了一个网格到场景组件然后我有点移动网格所以枢轴场景组件的点将位于门网格的左下角。我已经在角色中设置了一个功能,当他看着门并按下 e 键时将执行该功能我还在场景组件 (OnInteract) 中设置了一个功能,该功能应该将门旋转 90 度。问题是我需要从字符类中调用这个函数。

这里是 SceneComponent 中 OnInteract 函数的代码:

void UDoorSceneComponent::OnInteract()
{
    GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, TEXT("The function is called"));
    FRotator NewRotation = GetRelativeRotation();
    NewRotation.Yaw -= 90;
    SetRelativeRotation(NewRotation);
}

角色类中交互函数的代码:

void APlayerCharacter::Interact()
{
    FHitResult HitResult;

    //Makes an invisible line between the Start and End point
    FVector Start = FollowCamera->GetComponentLocation();
    FVector End = Start + FollowCamera->GetForwardVector() * InteractLengh;
    
    // if something is between the line then it stores the information in HitResult
    GetWorld()->LineTraceSingleByChannel(HitResult, Start, End, ECollisionChannel::ECC_Visibility);

    // Cast to Door. will return a nullptr if the object between Start and End wasnt the door
    ADoor1* DoorCast = Cast<ADoor1>(HitResult.GetActor());

    // if Door exists (not a nullptr) run the open door funcion
    if(DoorCast)
    {
        // Got the actor refrence
        AActor* Door1Act = UGameplayStatics::GetActorOfClass(GetWorld(), ADoor1::StaticClass());
        ADoor1* Door1Cast = Cast<ADoor1>(Door1Act);
        if(Door1Cast == nullptr)
        {
            GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, TEXT("nullptr"));
        }
        else
        {
            GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, TEXT("not nullptr"));
        }
    }


}

如您所见,我试图获得对门类的引用,但我不知道如何从这里执行 OnInteract 函数。我真的尝试在谷歌上查找它,但没有真正找到任何东西。我使用 manjaro linux 作为操作系统。 我也是 Unreal Engine 的新手,所以请放轻松 :) 谢谢

c++ game-development unreal-engine4 unreal-engine5
© www.soinside.com 2019 - 2024. All rights reserved.