Hey! So it turns out, this is a lot more complicated then I thought it would be: I am trying to shoot a projectile from the position and rotation of my camera. The problem is, that the projectile detects a “Hit” as soon as it spawns and therefore destroys itself (if you read the code, you will know what I am talking about). However, when I am looking at an angle that is slightly above looking straight forward and while standing still, the projectile is instantiated just the way I want it to. It seems to be hitting something that is attached to the player, but not the player…if that makes any sense Anyone know what I can do to solve this problem? Thank you in advance!
Cheers!
Here is the code I am using at the moment to instantiate the projectile:
void AFPSCharacter::OnFire()
{
const FVector cameraLocation = FirstPersonCameraComponent->GetComponentLocation();
const FRotator cameraRotation = FirstPersonCameraComponent->GetComponentRotation();
UWorld* const World = GetWorld();
if (World != NULL)
{
World->SpawnActor<AInstinctProjectile>(ProjectileClass, cameraLocation, cameraRotation);
}
}
Here is the projectile collision code:
void AFPSProjectile::OnHit(AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
{
if ((OtherActor != NULL) && (OtherActor != this) && (OtherComp != NULL))
{
if (!OtherActor->IsA(ACollectable::StaticClass()))
{
OtherComp->AddImpulseAtLocation(GetVelocity() * 100.0f, GetActorLocation());
Destroy();
}
if (OtherActor->IsA(AFPSCharacter::StaticClass()))
{
GiveDamage(Cast<AFPSCharacter>(OtherActor));
}
}
}