Line trace does not return true

FHitResult Hit;

bool bSuccess =

GetWorld() -> LineTraceSingleByChannel(

    Hit,

    Location,

    LineTraceEnd,

    ECollisionChannel::ECC_GameTraceChannel1

    );

FVector ParticleHitDirection = -Rotation.Vector();



if (bSuccess)

{

    UE_LOG(LogTemp, Warning, TEXT("Hit was a success"))

    FVector ShotDirection = -Rotation.Vector();

    AActor* HitActor = Hit.GetActor();

    if (HitActor != nullptr)

    {

        FPointDamageEvent DamageType(Damage, Hit, ShotDirection, nullptr);

        HitActor -> TakeDamage(Damage, DamageType, ShooterController, this);

    }

    UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), ImpactParticle, Hit.Location, ParticleHitDirection.Rotation());

}

}

This is my Gun.cpp code file. Linetrace doesnt return true for some reason, i was able to note that error by using the Log, i tried setting bSuccess to true and i got the Log warning i set up in the output log. While i was setting up my bullet trace channel, i did as said in the tutorial video, so i dont understand where the problem is coming from.

Here is the full code file, incase you need it

// Fill out your copyright notice in the Description page of Project Settings.
#include "Gun.h"
#include "Kismet/GameplayStatics.h"
#include "DrawDebugHelpers.h"

// Sets default values
AGun::AGun()
{
    // 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> ("Root");
    RootComponent = Root;
    Mesh = CreateDefaultSubobject<USkeletalMeshComponent> ("Mesh");
    Mesh -> SetupAttachment(Root);
}

// Called when the game starts or when spawned
void AGun::BeginPlay()
{
    Super::BeginPlay();
    
}

// Called every frame
void AGun::Tick(float DeltaTime)
{
    Super::Tick(DeltaTime);
}

void AGun::PullTrigger() 
{
    UGameplayStatics::SpawnEmitterAttached(ShootParticle, Mesh, TEXT("MuzzleFlashSocket"));

    ShooterCharacter = Cast<APawn>(GetOwner());
    if(!ShooterCharacter) {return;}

    ShooterController = ShooterCharacter -> GetController();
    if(!ShooterController) {return;}

    FVector Location;
    FRotator Rotation;
    ShooterController -> GetPlayerViewPoint(Location, Rotation);
    FVector LineTraceEnd = Location + Rotation.Vector() * MaxRange;
    
    FHitResult Hit;
    bool bSuccess =
    GetWorld() -> LineTraceSingleByChannel(
        Hit,
        Location,
        LineTraceEnd,
        ECollisionChannel::ECC_GameTraceChannel1
        );
    
    FVector ParticleHitDirection = -Rotation.Vector();
    
    if (bSuccess)
    {
        UE_LOG(LogTemp, Warning, TEXT("Hit was a success"))
        FVector ShotDirection = -Rotation.Vector();
        AActor* HitActor = Hit.GetActor();
        if (HitActor != nullptr)
        {
            FPointDamageEvent DamageType(Damage, Hit, ShotDirection, nullptr);
            HitActor -> TakeDamage(Damage, DamageType, ShooterController, this);
        }
        UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), ImpactParticle, Hit.Location, ParticleHitDirection.Rotation());
    }
}

Also, i think its important to mention that i had already moved on to other lectures, so some codes may be from the future lectures.

I’ve edited your post to use a code block instead of quotes. Please use it in the future.

Could you show the value for MaxRange (and also if you modified it in a blueprint) as well as your collision settings you added in your projects settings.

I did not edit the value of MaxRange in blueprint

For the collision settings, what are the important object channels that you would want to see, do i have to open each of them and send?

This is for PhysicsActor, which other one should i take a picture of?

Well the characters use Pawn (CapsuleComp) and CharacterMesh (SkeletalMesh) so if it’s not that would you mind sending your project by using the following link?

Please use File > Package Project > Zip Up Project for creating the zip as it will only include necessary files.

https://gdev.tv/projectupload

Apologies, i did not make my problem clear enough, LineTrace is returning true when i shoot at a character, but its not when i shoot at the wall (StaticMesh)

Do i need to upload the whole project, a 1.66 GB file size?

Privacy & Terms