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();
}