void ATheLeadGiverCharacter::StopFiring()
{
AM4A1* M4A1 = Cast(GetMesh());
if (M4A1)
{
M4A1->StopFiring();
}
}
void ATheLeadGiverCharacter::Fire()
{
AM4A1* M4A1 = Cast(GetMesh());
if (M4A1)
{
M4A1->Fire();
}
}
void ATheLeadGiverCharacter::BeginTouch(const ETouchIndex::Type FingerIndex, const FVector Location)
{
TouchItem.bIsPressed = true;
TouchItem.FingerIndex = FingerIndex;
TouchItem.Location = Location;
TouchItem.bMoved = false;
if (TouchItem.bIsPressed == true)
{
return;
}
if ((FingerIndex == TouchItem.FingerIndex) && (TouchItem.bMoved == false))
{
Fire();
//FireShot();
}
}
Here is where I am calling the functions within the gun.
Below is where I am placing the linetrace functionality.
void AM4A1::FireShot()
{
ATheLeadGiverCharacter* Player = Cast<ATheLeadGiverCharacter>(UGameplayStatics::GetPlayerCharacter(this, 0));
//Here is where I get stuck I don't know where to cast to the skeletal mesh inside of the character or should I just use the gun's skeletal mesh.
FHitResult* HitResult = new FHitResult();
FHitResult Hit;
const float WeaponRange = 20000.0f;
FVector CameraForward = FVector(Player->AssaultRifle->GetForwardVector());
const FVector StartTrace = Player->AssaultRifle->GetSocketLocation(FName("Muzzle1"));
const FVector EndTrace = (Player->AssaultRifle->GetForwardVector() * WeaponRange);
FCollisionQueryParams QueryParams = FCollisionQueryParams(SCENE_QUERY_STAT(WeaponTrace), false, this);
FCollisionQueryParams* TraceParams = new FCollisionQueryParams();
if (GetWorld()->LineTraceSingleByChannel(Hit, StartTrace, EndTrace, ECC_Visibility, QueryParams))
{
if (Hit.GetActor())
{
}
}
if (GetWorld()->LineTraceSingleByChannel(Hit, StartTrace, EndTrace, ECC_Visibility, QueryParams))
{
if (ImpactParticles)
{
UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), ImpactParticles, FTransform(Hit.ImpactNormal.Rotation(), Hit.ImpactPoint));
}
}
if (GetWorld()->LineTraceSingleByChannel(Hit, StartTrace, EndTrace, ECC_Visibility, QueryParams))
{
if (MuzzleFlash)
{
UGameplayStatics::SpawnEmitterAttached(MuzzleFlash, M4A1Mesh, (FName("Muzzle1")));
}
}
}
void AM4A1::Fire()
{
FireShot();
GetWorldTimerManager().SetTimer(FireTimer, this, &AM4A1::FireShot, ShotDelay, true);
//Here I am firing the gun itself.
}
void AM4A1::StopFiring()
{
GetWorldTimerManager().ClearTimer(FireTimer);
//Here I am stopping the automatic timer.
}
//I edited the code so that I could make the problem a little easier to solve and more concise picking up the gun is no longer a concern I just want to call the line trace function from the gun within the character. Picking up the gun is a secondary objective and is unnecessary to achieve this goal. Please forgive the edits.