Hi Class, I notice when clicking fire the impact is behind the player’s arm instead of hitting the wall. How can I correct this? I posted a Google link to view a video: Any feedback is appreciated
https://drive.google.com/file/d/1UhtrL4xlkPyfubxQAGhlxA5djDG93qJC/view?usp=sharing
FireArm.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "FireArm.h"
#include "Components/SkeletalMeshComponent.h"
#include "Kismet/GameplayStatics.h"
#include "DrawDebugHelpers.h"
// Sets default values
AFireArm::AFireArm()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
Root = CreateDefaultSubobject<USceneComponent>(TEXT("Root"));
SetRootComponent(Root);
Mesh = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("Mesh"));
Mesh->SetupAttachment(Root);
}
void AFireArm::PullTrigger()
{
UGameplayStatics::SpawnEmitterAttached(MuzzleFlash, Mesh, TEXT("MuzzleFlashSocket"));
APawn* OwnerPawn = Cast<APawn>(GetOwner());
if (OwnerPawn == nullptr) return;
AController* OwnerController = OwnerPawn->GetController();
if (OwnerController == nullptr) return;
FVector Location;
FRotator Rotation;
OwnerController->GetPlayerViewPoint(Location, Rotation);
FVector End = Location + Rotation.Vector() * MaxRange;
// TODO: LineTrace
FHitResult Hit;
bool bSuccess = GetWorld()->LineTraceSingleByChannel(Hit, Location, End, ECollisionChannel::ECC_GameTraceChannel1);
if (bSuccess)
{
FVector ShotDirection = -Rotation.Vector();
UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), ImpactEffect, Hit.Location, ShotDirection.Rotation());
}
}
// Called when the game starts or when spawned
void AFireArm::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AFireArm::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
FireArm.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "FireArm.generated.h"
UCLASS()
class DISCORDANT_API AFireArm : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AFireArm();
void PullTrigger();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
private:
UPROPERTY(VisibleAnyWhere)
USceneComponent* Root;
UPROPERTY(VisibleAnyWhere)
USkeletalMeshComponent* Mesh;
UPROPERTY(EditAnyWhere)
UParticleSystem* MuzzleFlash;
UPROPERTY(EditAnywhere)
UParticleSystem* ImpactEffect;
UPROPERTY(EditAnyWhere)
float MaxRange = 1000;
};