When i do linetracesinglebychannel the debugline does not appear

I have been using some code from the course to do a little game, the theme is that i want to do a melee combat and the linetracesinglebychannel fails and does not draw the debugline, theres a solution to that problem? here is the code

#include "TheLegend.h"

#include "Sword.h"

#include "LegendGameModeBase.h"

#include "Components/CapsuleComponent.h"

#include "Engine/TriggerVolume.h"

#include "Components/SkeletalMeshComponent.h"

#include "Kismet/GameplayStatics.h"

#include "DrawDebugHelpers.h"

//#include "PickUpActor.h"

// Sets default values

ATheLegend::ATheLegend()

{

    // Set this character to call Tick() every frame.  You can turn this off to improve performance if you don't need it.

    PrimaryActorTick.bCanEverTick = true;

   

}

// Called when the game starts or when spawned

void ATheLegend::BeginPlay()

{

    Super::BeginPlay();

    Health = MaxHealth;

   
       

}

// Called every frame

void ATheLegend::Tick(float DeltaTime)

{

    Super::Tick(DeltaTime);

}

// Called to bind functionality to input

void ATheLegend::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)

{

    Super::SetupPlayerInputComponent(PlayerInputComponent);

    PlayerInputComponent -> BindAxis(TEXT("MoveForward"), this,  &ATheLegend::MoveForward );

    PlayerInputComponent ->BindAxis(TEXT("MoveRight"), this,  &ATheLegend::MoveRight);

    PlayerInputComponent ->BindAxis(TEXT("LookUp"), this,  &ATheLegend::LookUp);

    PlayerInputComponent ->BindAxis(TEXT("LookRight"), this,  &ATheLegend::LookRight);

    PlayerInputComponent -> BindAction(TEXT("SwordAttack"), EInputEvent::IE_Pressed, this,  &ATheLegend::SwordAttack);

    PlayerInputComponent -> BindAction(TEXT("Jump"),  EInputEvent::IE_Pressed, this,  &ACharacter::Jump);

    //PlayerInputComponent -> BindAction(TEXT("MasterSlash"), EInputEvent::IE_Pressed, this, &ATheLegend::MasterSlash);

    //PlayerInputComponent -> BindAction(TEXT("ShieldBlock"), EInputEvent::IE_Pressed, this ,  &ATheLegend::ShieldBlock);

    //PlayerInputComponent -> BindAction(TEXT("Evade"), EInputEvent::IE_Pressed, this,  &ATheLegend::Evasion );

    //PlayerInputComponent -> BindAction(TEXT(SwitchItem, this, EInputEvent::IE_pressed, &ALegend::SwitchItem));

    //PlayerInputComponent -> BindAction(TEXT("Evasion"), this, EInputEvent::IE_Pressed, &ALegend::Evitate));

    //inventory

    //if(GetOwner())

    //Opens Inventory

    //click on the item you want

    // assing items to quick inventory with the arrows

}

void ATheLegend::SwordAttack()

{

     Hitting();

    UE_LOG(LogTemp, Warning, TEXT("Attacking"));

}

void ATheLegend::MoveForward(float AxisValue)

{

    AddMovementInput(GetActorForwardVector() * AxisValue);

}

void ATheLegend::MoveRight(float AxisValue)

{

    AddMovementInput(GetActorRightVector() * AxisValue);

}

void ATheLegend::LookRight(float AxisValue)

{

    AddControllerYawInput(AxisValue * RotationRate * GetWorld() -> GetDeltaSeconds());

}

void ATheLegend::LookUp(float AxisValue)

{

    AddControllerPitchInput(AxisValue * RotationRate * GetWorld() -> GetDeltaSeconds());

}

void ATheLegend::MasterSlash()

{

    //Damage = SwordDamage * 100;

    //MasterSlash();

}

void ATheLegend::ShieldBlock()

{

    //Block();

}

//void ATheLegend::Evasion()

//{

    //GetActorTransform()-> TransformLocation(GetActorTransform()+3);

       

       //if(evasiontime = 1)

       //FlurryRush();

         

//}

float ATheLegend::TakeDamage(float DamageAmount, FDamageEvent const& DamageEvent, AController* EventInstigator, AActor* DamageCauser)

{

    float DamageApllied = Super::TakeDamage(DamageAmount,DamageEvent, EventInstigator, DamageCauser);

    DamageApllied = FMath::Min(Health, DamageApllied * SwordDamage);

    Health -= DamageApllied;

    UE_LOG(LogTemp, Warning, TEXT("Health remaining is %f"), Health);

    if(bIsDead())

    {

        UE_LOG(LogTemp, Warning, TEXT("It died"));

        ALegendGameModeBase* GameMode =  GetWorld() -> GetAuthGameMode<ALegendGameModeBase>();

        if(GameMode != nullptr)

             {

                //GameMode -> PawnKilled(this);

             }

              DetachFromControllerPendingDestroy();

             GetCapsuleComponent() -> SetCollisionEnabled(ECollisionEnabled::NoCollision);

                          

    }

    return DamageApllied;

}

bool ATheLegend::bIsDead() const

{

    return Health <= 0;

}

//float ATheLegend::GetHealthPercent()

//{

    //return Health / MaxHealth;

//}

void ATheLegend::lastStandFury()

{

    if(Health < 30)

    {

     SwordDamage = 50;

    }

}

void ATheLegend::Hitting()

{

   

    FHitResult Hit;

    FVector SwordDirection;

    //GetWorld()

bool  bSuccess =  SwordTraceLine(Hit, SwordDirection);

if(bSuccess)

{

    UE_LOG(LogTemp, Warning, TEXT("Its workin"));

    DrawDebugPoint(GetWorld(), Hit.Location, 20, FColor::Red, true);

       

    AActor* HitActor = Hit.GetActor();

    if(HitActor != nullptr)

    {

        FPointDamageEvent DamageEvent( Damage, Hit, SwordDirection, nullptr);

        AController* OwnerController = GetOwnerController();

        HitActor-> TakeDamage( Damage,DamageEvent, OwnerController,  this);

        //health =health + 30;

    }

   

}

}

bool ATheLegend::SwordTraceLine(FHitResult &Hit, FVector &SwordDirection)

{

 AController* OwnerController = GetOwnerController();

   

    if(OwnerController == nullptr)return(false);

    FVector Location = GetActorLocation();

    FRotator Rotation = GetActorRotation();

    OwnerController-> GetPlayerViewPoint(Location,  Rotation);

   

     SwordDirection = -Rotation.Vector();

   

    FVector End = Location + Rotation.Vector() * MaxRange;

   
   

    FCollisionQueryParams Params;

    Params.AddIgnoredActor(this);

    Params.AddIgnoredActor(GetOwner());

    return GetWorld() -> LineTraceSingleByChannel(Hit,Location, End, ECollisionChannel::ECC_GameTraceChannel1, Params);

       

}

AController* ATheLegend::GetOwnerController() const

{

    APawn* OwnerPawn = Cast<APawn>(GetOwner());

    if(OwnerPawn == nullptr) return nullptr;

    return OwnerPawn -> GetController();

   

}

Please use the preformatted text button for code.
image
Also is the code even reaching the line trace? Have you tried adding logs?

i have added log to check if the line trace was working but it seems that it isnt (and sorry for the code i forgot about that code button)
image

And before SwordTraceLine?

when i log something before the trace line it works,i think bSuccess is the problem

That would suggest you aren’t hitting anything with your line trace. How have you set that up?

And this function is returning false so did you try debug it by adding print statements at points?

if you mean to draw debugpoint yes i have tried it and it does not show the point, if not what are you
referring to with adding print statements at points?

No I meant add logs to see where it’s returning false and why e.g.

if (OwnerController == nullptr)
{
    // log its null
    return false;
}

etc.

1 Like

when i press the click it logs nullptr so owner controller is being detected as null pointer, how i solve it?

Did you add further logs to see why that might be? Is the owner null? Or what?

i understanded the problem, the owner is null because its not attached to anything because i apllied the code to the character itself and not to an object attached(the sword)so it has no owner, so i solved it making a sword asset attached to the character and apllyng the code to it, but i still dont seeing the line

and i tried to log to see if it was working but its not

You are responsible for setting the owner for actors. You need to set it when you spawn it, attaching an actor to another does not set the owner.

i think I have explainned myself wrong, i have done that but the problem persist that is what im reffering to

Is the owner being null a guess or is it actually null? If the latter then I’m a little confused.

the owner is not null anymore, what i mean is that the linetrace is failing, i tryied to log to prove if it was working but its not

What did you try exactly?

Are you sure the line trace is correct? Are you using the right channel?

im using ths channel, how i know which channel use?, what i try is to do a line trace when i press the left click to deal damage to an enemy

Well that’s completely up to you to decide

Collision Filtering - Unreal Engine

Privacy & Terms