Hey guys,
Recently I’ve been working on some project that requires bow and arrow action. Long story short my idea was to do this:
- The bow is attached to a slot on a skeletal mesh.
- Create SERVER function on the bow that handles spawning and launching an arrow.
Part of this fire arrow function require getting the start location from which arrow will be spawned and fired. Here is the code:
//#Bow_Attack
//UFUNCTION(Server,Reliable,WithValidation)
void ARegularBow::FireArrow_Implementation()
{
//Check if bow has an owner and that weapon is not on cooldown
if (OwningChar && !bIsOnCooldown)
{
//Vector and rotator for camera trace
FVector CameraLoc;
FRotator CameraRot;
OwningChar->GetController()->GetPlayerViewPoint(CameraLoc, CameraRot);
//Vector and rotator for this actor.
FVector Loc = this->GetActorLocation();
FRotator Rot = this->GetActorRotation();
//Check for the world
UWorld* const World = GetWorld();
if (World)
{
//Spawn parameters for projectile
FActorSpawnParameters SpawnParams;
SpawnParams.Owner = OwningChar;
SpawnParams.Instigator = OwningChar;
//Spawn arrow
LastArrow = World->SpawnActor<AProjectileBase>(ProjectileToFire, Loc ,Rot ,SpawnParams);
if (LastArrow)
{
// find launch direction
FVector const LaunchDir = FVector(CameraLoc + CameraRot.Vector()*AimMaxDistance - this->GetActorLocation()).GetSafeNormal();
//Launch the arrow
LastArrow->FireInDirection(LaunchDir);
}
}
}
}
Here is where things get messy. When SERVER is not looking with the camera toward the client, camera frustum culling stops all the animations for that (client) object on the server. Having this in mind my variable FVector Loc = this->GetActorLocation(); return the wrong position of the bow and client fire arrow from a weird spot.
The problem mostly occurs when the round is finished and all characters respawn with animation that pulls character from the ground if the server does not look toward the client, client fire arrows from the ground.
Questions:
- Do you have any suggestions on how to
overcome this issue? - Is there a way to get the location of
the actor from the client but
execution to be on the server with
those values?
or
- Is there a way to exclude skeletal
mesh from camera frustum culling or
manually force animation updates on
the server?
Thanks!