196 - Spawning Projectiles Locally
This commit is contained in:
parent
b3f783a453
commit
fb79d80215
Binary file not shown.
Binary file not shown.
|
@ -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;
|
||||
|
||||
|
|
|
@ -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;
|
||||
FActorSpawnParameters SpawnParams;
|
||||
SpawnParams.Owner = GetOwner();
|
||||
SpawnParams.Instigator = InstigatorPawn;
|
||||
|
||||
UWorld* World = GetWorld();
|
||||
if (World)
|
||||
AProjectile* SpawnedProjectile = nullptr;
|
||||
if (bUseServerSideRewind)
|
||||
{
|
||||
if (InstigatorPawn->HasAuthority()) // Server
|
||||
{
|
||||
World->SpawnActor<AProjectile>(
|
||||
ProjectileClass,
|
||||
SocketTransform.GetLocation(),
|
||||
TargetRotation,
|
||||
SpawnParams
|
||||
);
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,4 +20,7 @@ private:
|
|||
UPROPERTY(EditAnywhere)
|
||||
TSubclassOf<class AProjectile> ProjectileClass;
|
||||
|
||||
UPROPERTY(EditAnywhere)
|
||||
TSubclassOf<AProjectile> ServerSideRewindProjectileClass;
|
||||
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue