diff --git a/Content/Blueprints/Character/BP_BlasterCharacter.uasset b/Content/Blueprints/Character/BP_BlasterCharacter.uasset index 8d48531..ab88639 100644 Binary files a/Content/Blueprints/Character/BP_BlasterCharacter.uasset and b/Content/Blueprints/Character/BP_BlasterCharacter.uasset differ diff --git a/Source/Blaster/Character/BlasterCharacter.cpp b/Source/Blaster/Character/BlasterCharacter.cpp index 8c7a501..a55b381 100644 --- a/Source/Blaster/Character/BlasterCharacter.cpp +++ b/Source/Blaster/Character/BlasterCharacter.cpp @@ -20,7 +20,7 @@ ABlasterCharacter::ABlasterCharacter() CameraBoom = CreateDefaultSubobject(TEXT("CameraBoom")); CameraBoom->SetupAttachment(GetMesh()); - CameraBoom->TargetArmLength = 600.f; + CameraBoom->TargetArmLength = 350.f; CameraBoom->bUsePawnControlRotation = true; FollowCamera = CreateDefaultSubobject(TEXT("FollowCamera")); diff --git a/Source/Blaster/Components/CombatComponent.cpp b/Source/Blaster/Components/CombatComponent.cpp index a483adf..432e7b5 100644 --- a/Source/Blaster/Components/CombatComponent.cpp +++ b/Source/Blaster/Components/CombatComponent.cpp @@ -7,11 +7,12 @@ #include "Blaster/Weapon/Weapon.h" #include "Engine/SkeletalMeshSocket.h" #include "GameFramework/CharacterMovementComponent.h" +#include "Kismet/GameplayStatics.h" #include "Net/UnrealNetwork.h" UCombatComponent::UCombatComponent() { - PrimaryComponentTick.bCanEverTick = false; + PrimaryComponentTick.bCanEverTick = true; BaseWalkSpeed = 600.f; AimWalkSpeed = 350.f; @@ -73,6 +74,52 @@ void UCombatComponent::FireButtonPressed(bool bPressed) } } +void UCombatComponent::TraceUnderCrosshairs(FHitResult& TraceHitResult) +{ + FVector2d ViewPortSize; + if (GEngine && GEngine->GameViewport) + { + GEngine->GameViewport->GetViewportSize(ViewPortSize); + } + + FVector2d CrosshairLocation(ViewPortSize.X / 2.f, ViewPortSize.Y / 2.0f); + FVector CrosshairWorldPosition; + FVector CrosshairWorldDirection; + bool bScreenToWorld = UGameplayStatics::DeprojectScreenToWorld( + UGameplayStatics::GetPlayerController(this, 0), + CrosshairLocation, + CrosshairWorldPosition, + CrosshairWorldDirection + ); + + if (bScreenToWorld) + { + FVector Start = CrosshairWorldPosition; + FVector End = Start + CrosshairWorldDirection * TRACE_LENGTH; + + GetWorld()->LineTraceSingleByChannel( + TraceHitResult, + Start, + End, + ECollisionChannel::ECC_Visibility + ); + if (!TraceHitResult.bBlockingHit) + { + TraceHitResult.ImpactPoint = End; + } + else + { + DrawDebugSphere( + GetWorld(), + TraceHitResult.ImpactPoint, + 12.f, + 12, + FColor::Red + ); + } + } +} + void UCombatComponent::ServerFire_Implementation() { MulticastFire(); @@ -92,6 +139,8 @@ void UCombatComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActo { Super::TickComponent(DeltaTime, TickType, ThisTickFunction); + FHitResult HitResult; + TraceUnderCrosshairs(HitResult); } void UCombatComponent::EquipWeapon(AWeapon* WeaponToEquip) diff --git a/Source/Blaster/Components/CombatComponent.h b/Source/Blaster/Components/CombatComponent.h index 0be7800..764dda3 100644 --- a/Source/Blaster/Components/CombatComponent.h +++ b/Source/Blaster/Components/CombatComponent.h @@ -6,6 +6,7 @@ #include "Components/ActorComponent.h" #include "CombatComponent.generated.h" +#define TRACE_LENGTH 80000 UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) ) class BLASTER_API UCombatComponent : public UActorComponent @@ -38,6 +39,8 @@ protected: UFUNCTION(NetMulticast, Reliable) void MulticastFire(); + + void TraceUnderCrosshairs(FHitResult& TraceHitResult); private: class ABlasterCharacter* Character;