Hi,
I am experiencing something very strange right now. I try to spawn a projectile from a gun, and always accelerate it to the speed of the gun + a delta (muzzle veloctiy).
//SPAWN PROJECTILE
const FVector location = ProjectileSpawn->GetComponentLocation();
const FRotator rotation = ProjectileSpawn->GetComponentRotation();
AActor* projectile = GetWorld()->SpawnActor(Ammunition->Projectile, &location, &rotation);
//GET ROOTS
UPrimitiveComponent* projectileRoot = Cast<UPrimitiveComponent>(projectile->GetRootComponent());
UPrimitiveComponent* gunRoot = Cast<UPrimitiveComponent>(GetAttachmentRoot());
//CALCULATE THE IMPULSE THAT RESULTS IN THE BULLET HAVING THE SAME SPEED AS THE GUN + A DELTA (muzzleVelocity)
const FVector gunVelocity = gunRoot->GetPhysicsLinearVelocityAtPoint(GetComponentLocation());
const FVector muzzleVelocity = rotation.Vector() * Ammunition->MuzzleSpeed;
const float projectileMass = projectileRoot->CalculateMass();
const FVector impulse = gunVelocity * projectileMass + muzzleVelocity * projectileMass;
//APPLY THAT IMPULSE
projectileRoot->AddImpulse(impulse);
//DEBUG
GEngine->AddOnScreenDebugMessage(-1, 3.f, FColor::Red,
TEXT("gunVelocity: ") + gunVelocity.ToString() +
TEXT("\nmuzzleVelocity: ") + muzzleVelocity.ToString() +
TEXT("\nimpulse gun part: ") + (gunVelocity * projectileMass).ToString() +
TEXT("\nimpulse muzzle part: ") + (muzzleVelocity * projectileMass).ToString() +
TEXT("\nimpulse: ") + impulse.ToString());
//APPLY RECOIL
gunRoot->AddImpulseAtLocation(100 * -impulse * Ammunition->recoilFactor, location);
The impulse does not get applied on the projectile. The recoil does get applied on the gun.
It gets even stranger: If add an AddImpulse in the tick function of my projectile blueprint, then this impulse gets applied AND the impulse of my c++ function also gets applied. But only at the same time as the tick function applies the impulse. If I apply the impulse in blueprint only after 30 frames, then both impulses will be delay by 30 frames. It seems like my first impulse gets somehow queued up, and executed with the next one? Please help!