When shooting on the client, the client’s projectile seems to be on a positive linear slope when firing in ADS. I take the ADS socket from the client and on the server it works perfectly. But the client its just slightly off on the Z-axis depening on the distance shot.
void AFPSCharacter::StartFire(const FInputActionValue& Val)
{
if (CanFire()) {
if (CurrentWeapon) {
bCanFire = false; // Disallow firing
// Only set a timer for automatic weapons
if (CurrentWeapon->bIsAutomatic) {
GetWorld()->GetTimerManager().SetTimer(FireTimerHandle, this, &AFPSCharacter::ResetFire, CurrentWeapon->FireRate, false);
}
if (HasAuthority())
{
Fire();
}
else
{
ServerStartFire();
}
}
}
}
void AFPSCharacter::StopFire()
{
if (CurrentWeapon && CurrentWeapon->bIsAutomatic) {
// For automatic weapons, clear the fire timer when the button is released
GetWorld()->GetTimerManager().ClearTimer(FireTimerHandle);
// And immediately allow firing again
bCanFire = true;
}
else if (CurrentWeapon) {
// For non-automatic weapons, reset fire immediately when the button is released
ResetFire();
}
}
void AFPSCharacter::ResetFire()
{
// Allow firing again when the timer expires, regardless of whether the weapon is automatic or not
bCanFire = true;
}
void AFPSCharacter::Fire()
{
if (bIsADS)
{
GetSocketTransform();
}
FTransform FiringPosition = GetFiringPosition();
// If not aiming down sights, spawn the projectile immediately.
SpawnProjectile(FiringPosition.GetLocation(), FiringPosition.GetRotation().Rotator());
}
void AFPSCharacter::ServerStartFire_Implementation()
{
Fire();
}
bool AFPSCharacter::ServerStartFire_Validate()
{
return true;
}
void AFPSCharacter::SpawnProjectile_Implementation(FVector SpawnLocation, FRotator SpawnRotation)
{
FActorSpawnParameters spawnParameters;
spawnParameters.Instigator = GetInstigator();
spawnParameters.Owner = this;
if (AProjectile* bullet = GetWorld()->SpawnActor<AProjectile>(ProjectileClass, SpawnLocation, SpawnRotation, spawnParameters))
{
FVector LaunchDirection = SpawnRotation.Vector();
LaunchDirection *= CurrentWeapon->WeaponDistance;
bullet->FireInDirection(LaunchDirection);
}
CurrentWeapon->AmmoInClip = FMath::Clamp(CurrentWeapon->AmmoInClip - 1, 0, CurrentWeapon->MaxAmmoInClip);
UE_LOG(LogTemp, Warning, TEXT("Location: X = %f, Y = %f, Z = %f"), SpawnLocation.X, SpawnLocation.Y, SpawnLocation.Z);
ApplyRecoil();
}
FTransform AFPSCharacter::GetFiringPosition()
{
if (bIsADS && IsLocallyControlled()) {
return BulletSocketTransform;
}
FVector SpawnLocation = GetActorLocation() + (GetControlRotation().Vector() * 100.0f) + (GetActorUpVector() * 50.0f);
FRotator SpawnRotation = GetControlRotation();
return FTransform(SpawnRotation, SpawnLocation);
}
void AFPSCharacter::GetSocketTransform()
{
if (this->WeaponMesh && IsLocallyControlled())
{
BulletSocketTransform = this->WeaponMesh->GetSocketTransform("frontSightPost", RTS_World);
}
}
I used debug spheres and the socket/spawn location of the projectile is correct , i replicate ADS correctly, everything is working as intened except the path of the projectile which I dont understand. This is UE ver 5.2.1