void ARifleWeapon::Fire() { Super::Fire(); if (Mesh1P && ThePlayer && ThePlayer->Controller) { /// Get Camera Location and Rotation FVector CamLoc; FRotator CamRot; ThePlayer->Controller->GetPlayerViewPoint(CamLoc, CamRot); // Get Mesh Fire Socket Location and calculatre Fire end point const FVector StartTrace = GetFireSocketTransform().GetLocation();; const FVector Direction = CamRot.Vector(); const FVector EndTrace = StartTrace + Direction *MainFire.FireDistance; // Set trace values and perform trace to retrieve hit info FCollisionQueryParams TraceParams(FName(TEXT("WeaponTrace")), true, this); TraceParams.bTraceAsyncScene = true; TraceParams.bReturnPhysicalMaterial = true; TraceParams.AddIgnoredActor(ThePlayer); // Trace Hit FHitResult Hit(ForceInit); if (GetWorld()->LineTraceSingleByChannel(Hit, StartTrace, EndTrace, ECC_WorldStatic, TraceParams)) { // Check if player character ARadeCharacter* EnemyPlayer = Cast<ARadeCharacter>(Hit.GetActor()); if (EnemyPlayer) { // Hit Enemy BP_HitEnemy(Hit); // Apply Damage UGameplayStatics::ApplyDamage(EnemyPlayer, MainFire.FireDamage, ThePlayer->Controller, Cast<AActor>(this), UDamageType::StaticClass()); } } } }
// Box Overlap Event void ASwordWeapon::OnSwordWeaponBoxBeginOverlap(AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult) { if ((OtherActor != nullptr) && (OtherActor != this) && (OtherActor != ThePlayer) && (OtherComp != nullptr) && GetOwner()!=OtherActor && Cast<ARadeCharacter>(OtherActor) != nullptr && bTracingMeleeAttack) { // Check if character was already slashed if (!HitActors.Contains(OtherActor)) { // Add Character to Slashed List HitActors.Add(Cast<ARadeCharacter>(OtherActor)); // BP Event BP_HitEnemy(SweepResult); // Apply Damage UGameplayStatics::ApplyDamage(Cast<ARadeCharacter>(OtherActor), MainFire.FireDamage, (ThePlayer && ThePlayer->Controller) ? ThePlayer->Controller:NULL , Cast<AActor>(this), UDamageType::StaticClass()); } } }
// Actual Melee Attack void AWeapon::MeleeAttack() { BP_MeleeAttack(); if (Mesh1P && ThePlayer && ThePlayer->Controller) { FVector CamLoc; FRotator CamRot; ThePlayer->Controller->GetPlayerViewPoint(CamLoc, CamRot); /// Get Camera Location and Rotation FVector Camoffset = ThePlayer->FirstPersonCameraComponent->RelativeLocation; FVector TraceStart = Camoffset + ThePlayer->GetActorLocation() - CamRot.Vector() * 20; FVector TraceEnd = Camoffset + ThePlayer->GetActorLocation() + CamRot.Vector() * MeleeAttackDistance; // Set trace values and perform trace to retrieve hit info FCollisionQueryParams TraceParams(FName(TEXT("Melee Weapon Trace")), true, this); TraceParams.bTraceAsyncScene = true; TraceParams.bReturnPhysicalMaterial = true; TraceParams.AddIgnoredActor(ThePlayer); TraceParams.AddIgnoredActor(this); FHitResult FHit(ForceInit); GetWorld()->LineTraceSingleByChannel(FHit, TraceStart, TraceEnd, ECC_WorldStatic, TraceParams); ARadeCharacter* EnemyPlayer = Cast<ARadeCharacter>(FHit.GetActor()); // Forward Hit Checking if (EnemyPlayer) { if (EnemyPlayer) { // Hit Enemy BP_HitEnemy(FHit); // Apply Damage UGameplayStatics::ApplyDamage(EnemyPlayer, MeleeAttackDamage, ThePlayer->Controller, Cast<AActor>(this), UDamageType::StaticClass()); } } else { // Right Hit Checking FHitResult RHit(ForceInit); FVector rightAngle = CamRot.Vector().RotateAngleAxis(MeleeAttackAngles, FVector::UpVector); rightAngle = Camoffset + ThePlayer->GetActorLocation() + rightAngle * MeleeAttackDistance; GetWorld()->LineTraceSingleByChannel(RHit, TraceStart, rightAngle, ECC_WorldStatic, TraceParams); EnemyPlayer = Cast<ARadeCharacter>(RHit.GetActor()); if (EnemyPlayer) { BP_HitEnemy(RHit); // Apply Damage UGameplayStatics::ApplyDamage(EnemyPlayer, MeleeAttackDamage, ThePlayer->Controller, Cast<AActor>(this), UDamageType::StaticClass()); } else { // Left Hit Checking FHitResult LHit(ForceInit); FVector leftAngle = CamRot.Vector().RotateAngleAxis(-MeleeAttackAngles, FVector::UpVector); leftAngle = Camoffset + ThePlayer->GetActorLocation() + leftAngle * MeleeAttackDistance; GetWorld()->LineTraceSingleByChannel(LHit, TraceStart, leftAngle, ECC_WorldStatic, TraceParams); EnemyPlayer = Cast<ARadeCharacter>(LHit.GetActor()); if (EnemyPlayer) { BP_HitEnemy(LHit); // Apply Damage UGameplayStatics::ApplyDamage(EnemyPlayer, MeleeAttackDamage, ThePlayer->Controller, Cast<AActor>(this), UDamageType::StaticClass()); } } } } }
void ARifleWeapon::Fire() { Super::Fire(); // Casting FVector CamLoc; FRotator CamRot; ThePlayer->Controller->GetPlayerViewPoint(CamLoc, CamRot); // Get the camera position and rotation const FVector StartTrace = Mesh1P->GetSocketLocation(TEXT("MuzzleFlashSocket")); // CamLoc; // trace start is the camera location const FVector Direction = CamRot.Vector(); const FVector EndTrace = StartTrace + Direction *MainFire.FireDistance; // Perform trace to retrieve hit info FCollisionQueryParams TraceParams(FName(TEXT("WeaponTrace")), true, this); TraceParams.bTraceAsyncScene = true; TraceParams.bReturnPhysicalMaterial = true; TraceParams.AddIgnoredActor(ThePlayer); //print("Fire Event"); FHitResult Hit(ForceInit); if (GetWorld()->LineTraceSingleByChannel(Hit, StartTrace, EndTrace, ECC_WorldStatic, TraceParams)) { //printr("Hit Something"); // if (GEngine) GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, Hit.GetActor()->GetName()); ABaseCharacter* EnemyPlayer = Cast<ABaseCharacter>(Hit.GetActor()); // typecast to the item class to the hit actor if (EnemyPlayer) { BP_HitEnemy(Hit); //printr("Apply Damage"); //EnemyPlayer->TakeDamage(Player, currentWeapon->MainDamage, 0); EnemyPlayer->TakeDamage(MainFire.FireDamage, FDamageEvent(), ThePlayer->Controller, ThePlayer); // Hit event // Spawn blood effect //UGameplayStatics::SpawnEmitterAtLocation(this, HitFX, Hit.Location, Hit.Normal.Rotation(), true); } } /* /// Beam Weapon if (Cast<UBeamWeapon>(currentWeapon)) { // Casting FVector CamLoc; FRotator CamRot; Cast<AInventoryTutorialCharacter>(Player)->Controller->GetPlayerViewPoint(CamLoc, CamRot); // Get the camera position and rotation const FVector StartTrace = Mesh->GetSocketLocation(TEXT("FireSocket")); // CamLoc; // trace start is the camera location const FVector Direction = CamRot.Vector(); const FVector EndTrace = StartTrace + Direction * Cast<UBeamWeapon>(currentWeapon)->MainFireDistance; // Perform trace to retrieve hit info FCollisionQueryParams TraceParams(FName(TEXT("WeaponTrace")), true, this); TraceParams.bTraceAsyncScene = true; TraceParams.bReturnPhysicalMaterial = true; TraceParams.AddIgnoredActor(Player); FHitResult Hit(ForceInit); if (GetWorld()->LineTraceSingle(Hit, StartTrace, EndTrace, ECC_WorldStatic, TraceParams)) { // if (GEngine) GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, Hit.GetActor()->GetName()); AWarrior* EnemyPlayer = Cast<AWarrior>(Hit.GetActor()); // typecast to the item class to the hit actor if (EnemyPlayer) { EnemyPlayer->MyTakeDamage(Player, currentWeapon->MainDamage, 0); if (GEngine) GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, Hit.BoneName.GetPlainNameString()); DrawDebugLine( GetWorld(), StartTrace, Hit.Location, FColor(255, 0, 0), false, 3, 0, 1 ); } } } */ /* // try and fire a projectile if (ProjectileClass != NULL) { const FRotator SpawnRotation = GetControlRotation(); // MuzzleOffset is in camera space, so transform it to world space before offsetting from the character location to find the final muzzle position const FVector SpawnLocation = GetActorLocation() + SpawnRotation.RotateVector(GunOffset); UWorld* const World = GetWorld(); if (World != NULL) { // spawn the projectile at the muzzle World->SpawnActor<AShooterProjectile>(ProjectileClass, SpawnLocation, SpawnRotation); } } // try and play the sound if specified if (FireSound != NULL) { UGameplayStatics::PlaySoundAtLocation(this, FireSound, GetActorLocation()); } */ }