71 - The Hit Target

This commit is contained in:
Kingsmedia 2022-05-05 00:39:17 +02:00
parent 87c38977c9
commit 53a50536f1
4 changed files with 54 additions and 2 deletions

View File

@ -20,7 +20,7 @@ ABlasterCharacter::ABlasterCharacter()
CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));
CameraBoom->SetupAttachment(GetMesh());
CameraBoom->TargetArmLength = 600.f;
CameraBoom->TargetArmLength = 350.f;
CameraBoom->bUsePawnControlRotation = true;
FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));

View File

@ -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)

View File

@ -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;