void AShotgun::ProjectileFire() { Super::ProjectileFire(); if (BulletClass != NULL) { FActorSpawnParameters SpawnParams; SpawnParams.Owner = this; SpawnParams.Instigator = Instigator; FVector BulletLoc = WeaponMesh->GetSocketLocation("BulletOrigin"); FRotator BulletRot = WeaponMesh->GetSocketRotation("BulletOrigin"); const int32 RandomSeed = FMath::Rand(); FRandomStream WeaponRandomStream(RandomSeed); const float SpreadCone = FMath::DegreesToRadians(WeaponConfig.WeaponSpread * 0.5); FVector ShootDir = WeaponRandomStream.VRandCone(BulletRot.Vector(), SpreadCone, SpreadCone); ShootDir.Z = 0; ABullet* const Bullet = GetWorld()->SpawnActor<ABullet>(BulletClass, BulletLoc, BulletRot, SpawnParams); if (Bullet) { Bullet->InitVelocity(ShootDir); Bullet->SetDamage(5.0f); } } bCanShoot = false; UWorld* const World = GetWorld(); if (World != NULL) { World->GetTimerManager().SetTimer(TimeHandle_ShootCooldownExpired, this, &AMyPawnWeapon::ShootCooldownExpired, WeaponConfig.TimeBetweenShots); } }
void ABaseWeapon::Instant_Fire() { const int32 RandomSeed = FMath::Rand(); FRandomStream WeaponRandomStream(RandomSeed); const float CurrentSpread = WeaponConfig.WeaponSpread; const float SpreadCone = FMath::DegreesToRadians(WeaponConfig.WeaponSpread * 0.5); const FVector AimDir = MyPawn->GetActorForwardVector(); const FVector StartTrace = MyPawn->GetActorLocation(); const FVector ShootDir = WeaponRandomStream.VRandCone(AimDir, SpreadCone, SpreadCone); FVector EndTrace; if (Target) { FVector TargetDir = (MyPawn->GetActorLocation() - Target->GetActorLocation()); TargetDir.Normalize(); FVector ShootDir2 = WeaponRandomStream.VRandCone(-TargetDir, SpreadCone, SpreadCone); EndTrace = StartTrace + ShootDir2 * WeaponConfig.WeaponRange; } else { EndTrace = StartTrace + ShootDir * WeaponConfig.WeaponRange; } const FHitResult Impact = WeaponTrace(StartTrace, EndTrace); SpawnParticle(EndTrace); HitActor = Cast<AActor>(Impact.GetActor()); //DrawDebugLine(GetWorld(), StartTrace, EndTrace, FColor::Red, false, 1.0f); if (HitActor) { Server_DealDamage(Impact, ShootDir, WeaponConfig); } }
void AWeapon::FireProjectile() { FHitResult ObjectHit(EForceInit::ForceInit); FVector CameraLocation; FRotator CameraRotation; WeaponOwner->Controller->GetPlayerViewPoint(CameraLocation, CameraRotation); const FVector TraceStart = CameraLocation; const FVector TraceDirection = CameraRotation.Vector(); FRandomStream WeaponRandomStream(FMath::Rand()); const float SpreadCone = FMath::DegreesToRadians(Config.Spread * 0.5); const FVector ShotDirection = WeaponRandomStream.VRandCone(TraceDirection, SpreadCone, SpreadCone); const FVector TraceEnd = TraceStart + ShotDirection * Config.Range; FCollisionQueryParams TraceParams(FName(TEXT("TraceShot")), true, this); TraceParams.AddIgnoredActor(WeaponOwner); //Weapon Recoil Calculation WeaponOwner->AddControllerPitchInput(-(Config.Recoil)); if (FMath::FRand() >= 0.65f) { WeaponOwner->AddControllerYawInput(Config.Recoil); } else { WeaponOwner->AddControllerYawInput(-(Config.Recoil)); } const UWorld* World = GetWorld(); //debug code start////////////////////////////////// if (GEngine) { //GEngine->AddOnScreenDebugMessage(0, 5.f, FColor::Yellow, ShotDirection.ToString()); } DrawDebugLine( World, TraceStart, TraceEnd, FColor(255, 0, 0), false, 3, 0, 3.0 ); //debug code end//////////////////////////////////// bool HitDetected = false; if (World) { HitDetected = World->LineTraceSingle(ObjectHit, TraceStart, TraceEnd, ECollisionChannel::ECC_WorldDynamic, TraceParams); if (HitDetected) { DoDamage(ObjectHit); } } }
void AWeapon::Instant_Fire() { const int32 RandomSeed = FMath::Rand(); FRandomStream WeaponRandomStream(RandomSeed); const float CurrentSpread = WeaponConfig.WeaponSpread; const float SpreadCone = FMath::DegreesToRadians(WeaponConfig.WeaponSpread * 0.5); const FVector AimDir = WeaponMesh->GetSocketRotation("MF").Vector(); const FVector StartTrace = WeaponMesh->GetSocketLocation("MF"); const FVector ShootDir = WeaponRandomStream.VRandCone(AimDir, SpreadCone, SpreadCone); const FVector EndTrace = StartTrace + ShootDir * WeaponConfig.WeaponRange; const FHitResult Impact = WeaponTrace(StartTrace, EndTrace); ProcessInstantHit(Impact, StartTrace, ShootDir, RandomSeed, CurrentSpread); }
void AShooterWeapon_Instant::FireWeapon() { const int32 RandomSeed = FMath::Rand(); FRandomStream WeaponRandomStream(RandomSeed); const float CurrentSpread = GetCurrentSpread(); const float ConeHalfAngle = FMath::DegreesToRadians(CurrentSpread * 0.5f); const FVector AimDir = GetAdjustedAim(); const FVector StartTrace = GetCameraDamageStartLocation(AimDir); const FVector ShootDir = WeaponRandomStream.VRandCone(AimDir, ConeHalfAngle, ConeHalfAngle); const FVector EndTrace = StartTrace + ShootDir * InstantConfig.WeaponRange; const FHitResult Impact = WeaponTrace(StartTrace, EndTrace); ProcessInstantHit(Impact, StartTrace, ShootDir, RandomSeed, CurrentSpread); CurrentFiringSpread = FMath::Min(InstantConfig.FiringSpreadMax, CurrentFiringSpread + InstantConfig.FiringSpreadIncrement); }
void ABaseWeapon::fireTargetingRaycasts() { const int32 RandomSeed = FMath::Rand(); FRandomStream WeaponRandomStream(RandomSeed); const float CurrentSpread = 2.0f; const float SpreadCone = FMath::DegreesToRadians(CurrentSpread * 0.5f); FVector Up_StartTrace = (MyPawn->GetActorLocation() + (MyPawn->GetActorForwardVector() * 500)) + (MyPawn->GetActorUpVector() * 30); FVector Up_Dir = MyPawn->GetActorForwardVector(); FVector Up_EndTrace = Up_StartTrace + Up_Dir * (WeaponConfig.WeaponRange); FVector Down_StartTrace = (MyPawn->GetActorLocation() + (MyPawn->GetActorForwardVector() * 500)) + (MyPawn->GetActorUpVector() * -30); FVector Down_Dir = MyPawn->GetActorForwardVector(); FVector Down_EndTrace = Down_StartTrace + Down_Dir * (WeaponConfig.WeaponRange); FVector Left_StartTrace = (MyPawn->GetActorLocation() + (MyPawn->GetActorForwardVector() * 500)) + (MyPawn->GetActorRightVector() * -30); FVector Left_Dir = MyPawn->GetActorForwardVector(); FVector Left_EndTrace = Left_StartTrace + Left_Dir * (WeaponConfig.WeaponRange); FVector Right_StartTrace = (MyPawn->GetActorLocation() + (MyPawn->GetActorForwardVector() * 500)) + (MyPawn->GetActorRightVector() * 30); FVector Right_Dir = MyPawn->GetActorForwardVector(); FVector Right_EndTrace = Right_StartTrace + Right_Dir * (WeaponConfig.WeaponRange); FName TraceTag("TraceTag"); FCollisionQueryParams TraceParams(TraceTag, true, this); TraceParams.bTraceAsyncScene = true; TraceParams.bReturnPhysicalMaterial = true; TraceParams.AddIgnoredActor(MyPawn); TraceParams.TraceTag = TraceTag; GetWorld()->LineTraceSingleByChannel(Up_hit, Up_StartTrace, Up_EndTrace, COLLISION_TARGET, TraceParams); //DrawDebugLine(GetWorld(), Up_StartTrace, Up_EndTrace, FColor::Cyan, false, -1.0f, 0, 2); GetWorld()->LineTraceSingleByChannel(Down_hit, Down_StartTrace, Down_EndTrace, COLLISION_TARGET, TraceParams); //DrawDebugLine(GetWorld(), Down_StartTrace, Down_EndTrace, FColor::Cyan, false, -1.0f, 0, 2); GetWorld()->LineTraceSingleByChannel(Left_hit, Left_StartTrace, Left_EndTrace, COLLISION_TARGET, TraceParams); //DrawDebugLine(GetWorld(), Left_StartTrace, Left_EndTrace, FColor::Cyan, false, -1.0f, 0, 2); GetWorld()->LineTraceSingleByChannel(Right_hit, Right_StartTrace, Right_EndTrace, COLLISION_TARGET, TraceParams); //DrawDebugLine(GetWorld(), Right_StartTrace, Right_EndTrace, FColor::Cyan, false, -1.0f, 0, 2); }
void AShooterWeapon_Instant::SimulateInstantHit(const FVector& ShotOrigin, int32 RandomSeed, float ReticleSpread) { FRandomStream WeaponRandomStream(RandomSeed); const float ConeHalfAngle = FMath::DegreesToRadians(ReticleSpread * 0.5f); const FVector StartTrace = ShotOrigin; const FVector AimDir = GetAdjustedAim(); const FVector ShootDir = WeaponRandomStream.VRandCone(AimDir, ConeHalfAngle, ConeHalfAngle); const FVector EndTrace = StartTrace + ShootDir * InstantConfig.WeaponRange; FHitResult Impact = WeaponTrace(StartTrace, EndTrace); if (Impact.bBlockingHit) { SpawnImpactEffects(Impact); SpawnTrailEffect(Impact.ImpactPoint); } else { SpawnTrailEffect(EndTrace); } }