196 - Spawning Projectiles Locally

This commit is contained in:
Kingsmedia 2022-05-27 23:33:52 +02:00
parent b3f783a453
commit fb79d80215
5 changed files with 51 additions and 20 deletions

View File

@ -24,6 +24,8 @@ public:
UPROPERTY(EditAnywhere)
float InitialSpeed = 15000;
float Damage = 20.f;
protected:
virtual void BeginPlay() override;
void StartDestroyTimer();
@ -35,9 +37,6 @@ protected:
virtual void OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit);
virtual void Destroyed() override;
UPROPERTY(EditAnywhere)
float Damage = 20.f;
UPROPERTY(EditAnywhere)
class UParticleSystem* ImpactParticles;

View File

@ -10,32 +10,61 @@ void AProjectileWeapon::Fire(const FVector& HitTarget)
{
Super::Fire(HitTarget);
if (!HasAuthority()) return;
APawn* InstigatorPawn = Cast<APawn>(GetOwner());
const USkeletalMeshSocket* MuzzleFlashSocket = GetWeaponMesh()->GetSocketByName(FName("MuzzleFlash"));
if (MuzzleFlashSocket)
UWorld* World = GetWorld();
if (MuzzleFlashSocket && World)
{
FTransform SocketTransform = MuzzleFlashSocket->GetSocketTransform(GetWeaponMesh());
// From muzzle flash socket to hit location from TraceUnderCrosshairs
FVector ToTarget = HitTarget - SocketTransform.GetLocation();
FRotator TargetRotation = ToTarget.Rotation();
if (ProjectileClass && InstigatorPawn)
{
FActorSpawnParameters SpawnParams;
SpawnParams.Owner = GetOwner();
SpawnParams.Instigator = InstigatorPawn;
UWorld* World = GetWorld();
if (World)
AProjectile* SpawnedProjectile = nullptr;
if (bUseServerSideRewind)
{
World->SpawnActor<AProjectile>(
ProjectileClass,
SocketTransform.GetLocation(),
TargetRotation,
SpawnParams
);
if (InstigatorPawn->HasAuthority()) // Server
{
if (InstigatorPawn->IsLocallyControlled()) // Server, host - use replicated projectile
{
SpawnedProjectile = World->SpawnActor<AProjectile>(ProjectileClass, SocketTransform.GetLocation(), TargetRotation, SpawnParams);
SpawnedProjectile->bUseServerSideRewind = false;
SpawnedProjectile->Damage = Damage;
}
else // Server, not locally controlled - spawn non-replicated projectile, no SSR
{
SpawnedProjectile = World->SpawnActor<AProjectile>(ServerSideRewindProjectileClass, SocketTransform.GetLocation(), TargetRotation, SpawnParams);
SpawnedProjectile->bUseServerSideRewind = false;
}
}
else // Client, using SSR
{
if (InstigatorPawn->IsLocallyControlled()) // Client, locally controlled - spawn non-replicated projectile, use SSR
{
SpawnedProjectile = World->SpawnActor<AProjectile>(ServerSideRewindProjectileClass, SocketTransform.GetLocation(), TargetRotation, SpawnParams);
SpawnedProjectile->bUseServerSideRewind = true;
SpawnedProjectile->TraceStart = SocketTransform.GetLocation();
SpawnedProjectile->InitialVelocity= SpawnedProjectile->GetActorForwardVector() * SpawnedProjectile->InitialSpeed;
SpawnedProjectile->Damage = Damage;
}
else // Client, not locally controlled - spawn non-replicated projectile, no SSR
{
SpawnedProjectile = World->SpawnActor<AProjectile>(ServerSideRewindProjectileClass, SocketTransform.GetLocation(), TargetRotation, SpawnParams);
SpawnedProjectile->bUseServerSideRewind = false;
}
}
}
else // Weapon not using SSR
{
if (InstigatorPawn->HasAuthority())
{
SpawnedProjectile = World->SpawnActor<AProjectile>(ProjectileClass, SocketTransform.GetLocation(), TargetRotation, SpawnParams);
SpawnedProjectile->bUseServerSideRewind = false;
SpawnedProjectile->Damage = Damage;
}
}
}

View File

@ -20,4 +20,7 @@ private:
UPROPERTY(EditAnywhere)
TSubclassOf<class AProjectile> ProjectileClass;
UPROPERTY(EditAnywhere)
TSubclassOf<AProjectile> ServerSideRewindProjectileClass;
};