I have a “gun” set up with a proxy zone and when the character enters the zone and while it is in the zone i want to shot at their location but i cant seam to manage it.
I have tried getting the character and fireing at ActorLocation but it just crashes
void AMyTurret::FireShot(){
// If we it's ok to fire again
if (bCanFire == true)
{
UWorld* const World = GetWorld();
// Get the camera transform
FVector CameraLoc = this->GetActorLocation();
FRotator CameraRot = this->GetActorRotation();
GetActorEyesViewPoint(CameraLoc, CameraRot);
// MuzzleOffset is in camera space, so transform it to world space before offsetting from the camera to find the final muzzle position
FVector const MuzzleLocation = CameraLoc + FTransform(CameraLoc).TransformVector(GunOffset);
FRotator MuzzleRotation = CameraRot;
ACharacter* EnemyCharacter = UGameplayStatics::GetPlayerCharacter(GetWorld(), 0);
if (World){
FActorSpawnParameters SpawnParams;
SpawnParams.Owner = this;
SpawnParams.Instigator = Instigator;
// spawn the projectile
AMyProjectile* Projectile = World->SpawnActor<AMyProjectile>(MuzzleLocation, MuzzleRotation, SpawnParams);
if (Projectile){
// find launch direction
FVector const LaunchDir = EnemyCharacter->GetActorLocation();//FVector(0.f, -2000.f, 0);
Projectile->InitVelocity(LaunchDir);
}
}
bCanFire = false;
World->GetTimerManager().SetTimer(TimerHandle_ShotTimerExpired, this, &AMyTurret::ShotTimerExpired, FireRate);
// try and play the sound if specified
if (FireSound != nullptr)
{
UGameplayStatics::PlaySoundAtLocation(this, FireSound, GetActorLocation());
}
bCanFire = false;
}
}