From 6c3072d6f9ea71f29e9bb453bf691ca0a6d7a562 Mon Sep 17 00:00:00 2001 From: TjgL Date: Wed, 22 Jan 2025 22:09:27 +0100 Subject: [PATCH] Fixed crash + added line trace when shooting --- Content/Legumix/BP_GameMode.uasset | 4 ++-- Content/Legumix/Player/BP_Play.uasset | 3 +++ Content/Legumix/Player/BP_Player.uasset | 4 ++-- .../Legumix/Weapon/Shotgun/BP_Revolver.uasset | 4 ++-- Source/LegumeMix/Private/Player/LMPlayer.cpp | 17 +++++++++++++++-- Source/LegumeMix/Private/Weapon/LMShotgun.cpp | 1 - .../LegumeMix/Private/Weapon/LMWeaponBase.cpp | 13 +++++++++++++ .../Private/Weapon/LMWeaponManager.cpp | 10 +--------- Source/LegumeMix/Public/Weapon/LMWeaponBase.h | 8 +++++++- 9 files changed, 45 insertions(+), 19 deletions(-) create mode 100644 Content/Legumix/Player/BP_Play.uasset diff --git a/Content/Legumix/BP_GameMode.uasset b/Content/Legumix/BP_GameMode.uasset index ae68f7c..c44688e 100644 --- a/Content/Legumix/BP_GameMode.uasset +++ b/Content/Legumix/BP_GameMode.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:6ef764f0d6d17fb2309efb34815b5a2714079966472e420a0d6181a5021a0b5e -size 21071 +oid sha256:e675e0cde9b82aaa2171be4f5bdaafc7ec19a9b3ca674a5d06f6756741c0ed01 +size 20983 diff --git a/Content/Legumix/Player/BP_Play.uasset b/Content/Legumix/Player/BP_Play.uasset new file mode 100644 index 0000000..d907bc7 --- /dev/null +++ b/Content/Legumix/Player/BP_Play.uasset @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:25d6d112aa65ffa05aee3109028ba543898d1a9acf24646f6d6171ce7ead5945 +size 50606 diff --git a/Content/Legumix/Player/BP_Player.uasset b/Content/Legumix/Player/BP_Player.uasset index 4d5bcfa..2f17b6b 100644 --- a/Content/Legumix/Player/BP_Player.uasset +++ b/Content/Legumix/Player/BP_Player.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:de3853f2ca8bfb65e3452717604b954e6be0282eec2300b16225233c159b4838 -size 42996 +oid sha256:753ea5e4b6acedce454e964d906df3a3a0532fa7f9c6535a87505bf4e8f60a21 +size 60088 diff --git a/Content/Legumix/Weapon/Shotgun/BP_Revolver.uasset b/Content/Legumix/Weapon/Shotgun/BP_Revolver.uasset index e2b2ae0..b9b9428 100644 --- a/Content/Legumix/Weapon/Shotgun/BP_Revolver.uasset +++ b/Content/Legumix/Weapon/Shotgun/BP_Revolver.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:05cad08ea69ef270f1838c74ae098b03e1a419febe857b984e747987ab20b93b -size 34483 +oid sha256:3cf116185fa5ffa77f1c1b0bcd58ced74d48270ec611a0bdf2bbb9afc6c00f4c +size 32168 diff --git a/Source/LegumeMix/Private/Player/LMPlayer.cpp b/Source/LegumeMix/Private/Player/LMPlayer.cpp index 9396841..834c8a3 100644 --- a/Source/LegumeMix/Private/Player/LMPlayer.cpp +++ b/Source/LegumeMix/Private/Player/LMPlayer.cpp @@ -60,10 +60,23 @@ void ALMPlayer::PlayAnimation(UAnimMontage* Animation) void ALMPlayer::FireBullets(const FLMBulletInfo Settings) { FVector EndLocation = Settings.Origin + (Settings.Direction * Settings.MaxDistance); - TArray Hits; - DrawDebugLineTraceMulti(GetWorld(), Settings.Origin, EndLocation, EDrawDebugTrace::ForDuration, true, Hits, FColor::Green, FColor::Red, 10.f); + TArray Hits = TArray(); + GetWorld()->LineTraceMultiByChannel(Hits, Settings.Origin, EndLocation, ECC_Camera); + DrawDebugLineTraceMulti(GetWorld(), Settings.Origin, EndLocation, EDrawDebugTrace::ForDuration, Hits.Num() > 0, Hits, FColor::Green, FColor::Red, 10.f); } +FVector ALMPlayer::GetWeaponFiringOrigin() const +{ + if (!Camera) + { + GEngine->AddOnScreenDebugMessage(INDEX_NONE, 10.f, FColor::Silver, TEXT("No camera ???")); + return FVector::ZeroVector; + } + return Camera->GetComponentTransform().GetLocation(); +} +FVector ALMPlayer::GetAimVector() const { return Camera->GetForwardVector(); } + + void ALMPlayer::Tick(float DeltaTime) { Super::Tick(DeltaTime); diff --git a/Source/LegumeMix/Private/Weapon/LMShotgun.cpp b/Source/LegumeMix/Private/Weapon/LMShotgun.cpp index f379187..1382d63 100644 --- a/Source/LegumeMix/Private/Weapon/LMShotgun.cpp +++ b/Source/LegumeMix/Private/Weapon/LMShotgun.cpp @@ -15,7 +15,6 @@ ALMShotgun::ALMShotgun() void ALMShotgun::PrimaryFire() { - ALMPlayer* Player = Cast(GetOuter()); if (!Player) return; diff --git a/Source/LegumeMix/Private/Weapon/LMWeaponBase.cpp b/Source/LegumeMix/Private/Weapon/LMWeaponBase.cpp index 3f8abb4..c0e78dd 100644 --- a/Source/LegumeMix/Private/Weapon/LMWeaponBase.cpp +++ b/Source/LegumeMix/Private/Weapon/LMWeaponBase.cpp @@ -4,6 +4,7 @@ #include "Weapon/LMWeaponBase.h" #include "Components/AudioComponent.h" +#include "Player/LMPlayer.h" ALMWeaponBase::ALMWeaponBase() @@ -27,6 +28,18 @@ void ALMWeaponBase::BeginPlay() } +void ALMWeaponBase::Setup(USkeletalMeshComponent* Mesh, AActor* CharOwner) +{ + SetActorHiddenInGame(true); + FAttachmentTransformRules Rules = FAttachmentTransformRules(EAttachmentRule::SnapToTarget, + EAttachmentRule::KeepRelative, + EAttachmentRule::KeepRelative, + false); + + AttachToComponent(Mesh, Rules); + Player = Cast(CharOwner); +} + void ALMWeaponBase::Reload() { } diff --git a/Source/LegumeMix/Private/Weapon/LMWeaponManager.cpp b/Source/LegumeMix/Private/Weapon/LMWeaponManager.cpp index d2ea688..043998e 100644 --- a/Source/LegumeMix/Private/Weapon/LMWeaponManager.cpp +++ b/Source/LegumeMix/Private/Weapon/LMWeaponManager.cpp @@ -27,16 +27,8 @@ void ULMWeaponManager::Initialize(USkeletalMeshComponent* Mesh) ALMWeaponBase* Instance = GetWorld()->SpawnActor(WeaponTemplate); GEngine->AddOnScreenDebugMessage(INDEX_NONE, 2.f, FColor::Blue, TEXT("Spawing")); - Instance->SetActorHiddenInGame(true); - + Instance->Setup(ArmsMesh, GetOwner()); Weapons.Add(Instance); - - FAttachmentTransformRules Rules = FAttachmentTransformRules(EAttachmentRule::SnapToTarget, - EAttachmentRule::KeepRelative, - EAttachmentRule::KeepRelative, - false); - - Instance->AttachToComponent(ArmsMesh, Rules); } if (!Weapons.IsEmpty()) diff --git a/Source/LegumeMix/Public/Weapon/LMWeaponBase.h b/Source/LegumeMix/Public/Weapon/LMWeaponBase.h index 4c1c3d5..1cbeece 100644 --- a/Source/LegumeMix/Public/Weapon/LMWeaponBase.h +++ b/Source/LegumeMix/Public/Weapon/LMWeaponBase.h @@ -6,10 +6,11 @@ #include "LMAmmo.h" #include "LMWeaponDataStructure.h" #include "GameFramework/Actor.h" -#include "GameFramework/SpringArmComponent.h" #include "LMWeaponBase.generated.h" +class ALMPlayer; + UCLASS() class LEGUMEMIX_API ALMWeaponBase : public AActor { @@ -18,6 +19,7 @@ class LEGUMEMIX_API ALMWeaponBase : public AActor public: ALMWeaponBase(); virtual void BeginPlay() override; + void Setup(USkeletalMeshComponent* Mesh, AActor* CharOwner); public: UFUNCTION(BlueprintCallable) @@ -70,6 +72,10 @@ protected: /* Weapon Data */ UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="Legumix|Weapon", meta=(AllowPrivateAccess=true)) int ClipAmmo; + UPROPERTY(VisibleInstanceOnly, BlueprintReadWrite, Category=Legumix) + TObjectPtr Player; + + private: UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=Legumix, meta=(AllowPrivateAccess=true)) FDataTableRowHandle DataTableRow;