Hello.
I have a class for Projectiles: when the Left Mouse Button is clicked the Projectile spawns and (should) go towards the crosshair’s direction.
Instead, in my case, it goes down a bit:
Here’s the code:
/* Called on LEFT MOUSE BUTTON click */
void AWeapon_Projectile::FireWeapon()
{
// Get shoot direction (GetBaseAimRotation)
// MyOwner is an ACharacter
FVector ShootDir = MyOwner->GetBaseAimRotation().Vector();
// Spawn projectile at WeaponSocket on the [SERVER]
ServerFireProjectile(ShootDir);
}
void AWeapon_Projectile::ServerFireProjectile_Implementation(FVector ShootDir)
{
/*****************/
/****** BUG ****** ---> The projectile spawns on the WeaponSocket, and its direction IS NOT the Crosshair direction!
/*****************/
// Here we say that the Projectile must spawn at a given SocketLocation
FTransform SpawnTM(MyOwner->GetMesh()->GetSocketLocation((FName("WeaponSocket"))));
AProjectile* Projectile = Cast<AProjectile>(UGameplayStatics::BeginDeferredActorSpawnFromClass(this, ProjectileClass, SpawnTM));
if (Projectile)
{
Projectile->Instigator = Instigator;
Projectile->SetOwner(this);
// Here, in the InitVelocity, I pass the ShootDir variable
Projectile->InitVelocity(ShootDir);
UGameplayStatics::FinishSpawningActor(Projectile, SpawnTM);
}
}
/* And here is the InitVelocity() function of the Projectile class */
void AProjectile::InitVelocity(FVector& ShootDirection)
{
// Setting the speed
MovementComp->InitialSpeed = 5.0f;
MovementComp->MaxSpeed = 5.0f;
// HERE WE USE THE ShootDirection
MovementComp->Velocity = ShootDirection * MovementComp->InitialSpeed;
}
What am I doing wrong?
Thanks in advance!