Download

Shooting a projectile from my player camera?

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 :confused: 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));
		}
	}
}

It is usually useful to look at the actual object that you are hitting to see the cause of things like collision hits. Are you sure it isn’t the player? Or another collision object that is relative to the player? Can you post the details of the object you hit? You’ll likely find that its either the player, or another collision object associated with it (like a weapon collider or some such).

Yeah good point, I thought the OtherActor->IsA(AFPSCharacter::StaticClass() would take care of that. Anyway, how would I be able to debug that? Could you give me a tipp?