Switched Gadget Attachment Point to Players Camera

This commit is contained in:
Rafal Swierczek 2023-09-30 19:29:41 +01:00
parent 9c2a1abeab
commit 5e36b9e25e
5 changed files with 23 additions and 8 deletions

View File

@ -54,7 +54,13 @@ void AEndlessVendettaCharacter::BeginPlay()
GadgetManager = Cast<AGadgetManager>(GadgetManagerActor); GadgetManager = Cast<AGadgetManager>(GadgetManagerActor);
FAttachmentTransformRules AttachmentRules(EAttachmentRule::SnapToTarget, true); FAttachmentTransformRules AttachmentRules(EAttachmentRule::SnapToTarget, true);
GadgetManagerActor->AttachToComponent(GetRootComponent(), AttachmentRules); GadgetManagerActor->AttachToComponent(GetRootComponent(), AttachmentRules);
GadgetManager->SpawnGadgets(GetRootComponent()); for (UActorComponent* PlayersCamera : GetComponentsByTag(UCameraComponent::StaticClass(), FName("PlayersCamera")))
{
UE_LOG(LogTemp, Warning, TEXT("An actor component with PlayersCamera tag has been found"));
GadgetManager->SpawnGadgets(Cast<USceneComponent>(PlayersCamera));
break;
}
} }
//////////////////////////////////////////////////////////////////////////// Input //////////////////////////////////////////////////////////////////////////// Input

View File

@ -32,6 +32,9 @@ protected:
UPROPERTY(EditDefaultsOnly, Category = "Gadget") UPROPERTY(EditDefaultsOnly, Category = "Gadget")
FVector EquippedOffset = FVector(0, 0, 0); FVector EquippedOffset = FVector(0, 0, 0);
UPROPERTY(EditDefaultsOnly, Category = "Gadget")
FRotator GadgetRotation = FRotator(0, 0, 0);
UPROPERTY(EditDefaultsOnly, CAtegory = "Gadget") UPROPERTY(EditDefaultsOnly, CAtegory = "Gadget")
FVector UnequippedOffset = FVector(0, 0, -1000); FVector UnequippedOffset = FVector(0, 0, -1000);
@ -74,6 +77,10 @@ public:
return UnequippedOffset; return UnequippedOffset;
} }
FRotator GetGadgetSpawnRotation()
{
return GadgetRotation;
}
// Sets default values for this actor's properties // Sets default values for this actor's properties
AGadgetBase(); AGadgetBase();

View File

@ -25,7 +25,7 @@ void AGadgetManager::Tick(float DeltaTime)
} }
void AGadgetManager::SpawnGadgets(USceneComponent* PlayersRootComponent) void AGadgetManager::SpawnGadgets(USceneComponent* PlayersCameraComponent)
{ {
if (!IsValid(ReconClass) || !IsValid(CombatClass)) UE_LOG(LogTemp, Fatal, TEXT("Recon or Combat class hasn't been set")); if (!IsValid(ReconClass) || !IsValid(CombatClass)) UE_LOG(LogTemp, Fatal, TEXT("Recon or Combat class hasn't been set"));
@ -35,11 +35,13 @@ void AGadgetManager::SpawnGadgets(USceneComponent* PlayersRootComponent)
AActor* SpawnedActor = GetWorld()->SpawnActor<AActor>(ReconClass, GetActorLocation(), GetActorRotation(), SpawnParams); AActor* SpawnedActor = GetWorld()->SpawnActor<AActor>(ReconClass, GetActorLocation(), GetActorRotation(), SpawnParams);
ReconGadget = Cast<AReconGadget>(SpawnedActor); ReconGadget = Cast<AReconGadget>(SpawnedActor);
SpawnedActor->AttachToComponent(PlayersRootComponent, AttachmentRules); SpawnedActor->AttachToComponent(PlayersCameraComponent, AttachmentRules);
SpawnedActor->SetActorRelativeLocation(ReconGadget->GetUnequippedOffset()); SpawnedActor->SetActorRelativeLocation(ReconGadget->GetUnequippedOffset());
SpawnedActor->SetActorRelativeRotation(ReconGadget->GetGadgetSpawnRotation());
SpawnedActor = GetWorld()->SpawnActor<AActor>(CombatClass, GetActorLocation(), GetActorRotation(), SpawnParams); SpawnedActor = GetWorld()->SpawnActor<AActor>(CombatClass, GetActorLocation(), GetActorRotation(), SpawnParams);
CombatGadget = Cast<ACombatGadget>(SpawnedActor); CombatGadget = Cast<ACombatGadget>(SpawnedActor);
SpawnedActor->AttachToComponent(PlayersRootComponent, AttachmentRules); SpawnedActor->AttachToComponent(PlayersCameraComponent, AttachmentRules);
SpawnedActor->SetActorRelativeLocation(CombatGadget->GetUnequippedOffset()); SpawnedActor->SetActorRelativeLocation(CombatGadget->GetUnequippedOffset());
SpawnedActor->SetActorRelativeRotation(CombatGadget->GetGadgetSpawnRotation());
} }

View File

@ -26,7 +26,7 @@ protected:
virtual void BeginPlay() override; virtual void BeginPlay() override;
public: public:
void SpawnGadgets(USceneComponent* PlayersRootComponent); void SpawnGadgets(USceneComponent* PlayersCameraComponent);
// Sets default values for this actor's properties // Sets default values for this actor's properties
AGadgetManager(); AGadgetManager();