I am trying to check if the linetrace only hit a particular object if it is true then play a debug message. I am using the boolean that the line trace uses to reference with a particular object that was hit, rather then using just checking to see if the linetrace hit, or not. In order to check the to see if the object was hit would I have to use a boxcomponent or sphere component? Do I have to write an OnHitFunction or an OnComponentBeginOverlapFunction to check to see if the Linetrace collided with the overlap? Also for some reason if I try to call a Destroy Function within the Object itself then call the function using the Linetrace the engine crashes. I feel like I am close to the truth somewhere.
void ACharacter::Linetrace()
{
//Here is the linetrace.
FHitResult* HitResult = new FHitResult();
FHitResult Hit;
const float LineTraceLength = 20000.0f;
const FVector StartTrace = FirstPersonCameraComponent->GetComponentLocation();
const FVector EndTrace = (FirstPersonCameraComponent->GetForwardVector() * WeaponRange);
FCollisionQueryParams QueryParams = FCollisionQueryParams(SCENE_QUERY_STAT(WeaponTrace),
false, this);
FCollisionQueryParams* TraceParams = new FCollisionQueryParams();
//Here is the linetrace itself.
if (GetWorld()->LineTraceSingleByChannel(Hit, StartTrace, EndTrace, ECC_Visibility, QueryParams))
{
if (Particles)
{
UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), ImpactParticles,
FTransform(Hit.Normal.Rotation(), Hit.EndPoint));
}
}
bHit = GetWorld()->LineTraceSingleByChannel(*HitResult, StartTrace, EndTrace, ECC_Visibility,
*TraceParams);
AAParticularObject* Object = Cast<AAParticularObject>(GetWorld());
//I am trying to check to see if the linetrace detects the character.
if (bHit /* = Object */ ) //Here is the condition. I know the condition is not correct this is just an example.
{
//I know you have to create a boolean. This is just the general idea.
//Object ->AIBlast();
//DrawDebugLine(GetWorld(), StartTrace, EndTrace, FColor(255, 0, 0), true);
//UE_LOG(LogTemp, Warning, TEXT("AActorHit"));
//I am calling a function within the character.
Object->ObjectBlast();
}
//This is the function I am calling within the character.
void AParticularObject::ObjectBlast()
{
AACharacter* Character= Cast<AACharacter>(GetWorld());
if (Character->bHit) //Checks to see if the bHit is equal to true.
{
UE_LOG(LogTemp, Warning, TEXT("ObjectHit"));
//Destroy(); is something I would like to call here without crashing the engine.
//I don't know if I have to use a Box Component or Sphere Component in order to resolve the issue.
}
}