Multiplayer shooting system not working

// AGun.cpp

#include “Gun.h”

#include “Components/StaticMeshComponent.h”

#include “Kismet/GameplayStatics.h”

#include “Net/UnrealNetwork.h”

#include “Particles/ParticleSystemComponent.h”

AGun::AGun()

{

PrimaryActorTick.bCanEverTick = true;

Root = CreateDefaultSubobject<USceneComponent>(TEXT("Root"));

SetRootComponent(Root);

GunMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("GunMesh"));

GunMesh->SetupAttachment(Root);

bReplicates = true;

SetReplicates(true);

SetReplicateMovement(true);

SetReplicatingMovement(true);

}

void AGun::BeginPlay()

{

Super::BeginPlay();

}

void AGun::GetLifetimeReplicatedProps(TArray &OutLifetimeProps) const

{

Super::GetLifetimeReplicatedProps(OutLifetimeProps);

DOREPLIFETIME(AGun, TraceDistance);

// DOREPLIFETIME(AGun, ImpactEmitter);

}

void AGun::Tick(float DeltaTime)

{

Super::Tick(DeltaTime);

}

bool AGun::ServerPerformLineTrace_Validate()

{

return true;

}

void AGun::ServerPerformLineTrace_Implementation()

{

// if(HasAuthority())

PerformLineTrace();

}

void AGun::PerformLineTrace()

{

// if (HasAuthority())

// {

    // APawn *OwnerPawn = Cast<APawn>(GetOwner());

    FVector Location = GetActorLocation();

    // OwnerPawn->GetController()->GetPlayerViewPoint(Location, Rotation);

    FVector End = Location + (-GetActorForwardVector())   * TraceDistance;

    FHitResult HitResult;

    FCollisionQueryParams CollisionParams;

    // CollisionParams.AddIgnoredActor(this);

    if (GetWorld()->LineTraceSingleByChannel(HitResult, Location, End, ECC_Visibility, CollisionParams))

    {

        DrawDebugLine(GetWorld(), Location, End, FColor::Red, false, 2.f, 1, 5.f);

        UE_LOG(LogTemp, Error, TEXT("%s"), *HitResult.GetActor()->GetName());

    }

// }

// else

// {

//     ServerPerformLineTrace();

// }

}

void AGun::Shoot()

{

if(HasAuthority())

{

    PerformLineTrace();

}

else

{

    ServerPerformLineTrace();

}

}

There’s no information here. A dump of code and saying it doesn’t work doesn’t help.

To be honest this isn’t the place to ask as it isn’t part of our courses. You should post a question in the discord…

Yoy could also download the shooter multiplayer sample Lyra from epic and examine the code as well.

Saying that, you’ll probably need an RPC on a fire event so the server knows to line trace otherwise the server won’t know you’re shooting. Then, on hit you’ll have to send out updates to the player health.

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms