I’m making a top down kinda isometric game and I’m trying to have the player fire projectiles. I’m making the player and the gun separate actors. The player class attaches the gun to a bone socket and then tells it when to fire.The issue is that it’s not firing in the direction the player is facing. Here is the firing code:
UWorld* const World = GetWorld();
if (bFiring && FireCoolDownTimer < World->GetTimeSeconds())
{
if (World)
{
if (ProjectileClass != NULL)
{
// Get the positions transforms
// Get the camera transform
FVector Loc = GetActorLocation();
FRotator Rot = GetAttachParentActor()->GetActorRotation();
FVector const MuzzleLocation = Loc + MuzzleOffset;
FActorSpawnParameters SpawnParams;
SpawnParams.Owner = this;
SpawnParams.Instigator = Instigator;
// spawn the projectile at the muzzle
AProjectile* const Projectile = World->SpawnActor<AProjectile>(ProjectileClass, MuzzleLocation, Rot, SpawnParams);
if (Projectile)
{
FVector const LaunchDir = Rot.Vector();
Projectile->InitVelocity(LaunchDir);
}
}//projectile check end
}//world check end
FireCoolDownTimer = World->GetTimeSeconds() + FireCoolDown;
}//bfiring check end
The “InitVelocity” code is:
void AProjectile::InitVelocity(const FVector& ShootDirection)
{
if (ProjectileMovement)
{
// set the projectile's velocity to the desired direction
ProjectileMovement->Velocity = ShootDirection * ProjectileMovement->InitialSpeed;
}
}