Hi.
So I think I know what I want to do but I’m having trouble with how to actually do it in blueprints, especially the screen to world location node.
Basically, I want to spawn a projectile around my characters arm that has it’s rotation modified to shoot to the middle of the screen. Since using a set rotation won’t accurately fire into the middle (as distance is also a factor into how much you’d want it to rotate), I was thinking of using the middle of the screen’s position, converting to a world location and getting the angle from my players arm to that position in space. Think of having a laser flying straight forward from the camera view and the first thing it hits it’s where the projectile aims towards.
Here’s a really simple image to show what I’m going for.
How would I set this up with blueprints? Or if there’s a more efficient way to get a projectile to travel accurately to the middle of the screen, what would that be?
Thanks for reading.
I did this for my project and it seems to work pretty well. Basically, just raycast straight out from the camera location, then use the hit location to calculate a rotation offset for the projectile. I implemented it in code, but maybe you could use it to create a blueprint version.
// Setup raycast query parameters used in this function
FCollisionQueryParams queryParams;
queryParams.bTraceComplex = true;
queryParams.AddIgnoredActor(this);
queryParams.AddIgnoredActor(GetOwner());
// Calculate reticle target
FHitResult hitResult;
queryParams.TraceTag = FName(TEXT("ReticleTarget"));
FVector maxReticleTargetExtent = GetTargetingSystemLocation() + GetTargetingSystemDirection() * maxTargetingDist;
bool bHit = w->LineTraceSingleByProfile(
hitResult,
GetTargetingSystemLocation(),
maxReticleTargetExtent,
projectileCollisionProfile,
queryParams);
FVector reticleTargetLocation = bHit ? hitResult.ImpactPoint : maxReticleTargetExtent;
#if !UE_BUILD_SHIPPING
if (AProjectile::bDebugProjectiles)
{
DrawDebugSphere(w, reticleTargetLocation, 100.f, 8.f, FColor::Red, true, 1.f);
}
#endif //!UE_BUILD_SHIPPING
// Calculate projectile spawn rotation
FName muzzleSocket = *FString::Printf(TEXT("Muzzle%d"), muzzleIndex);
FVector muzzleLocation = mesh->GetSocketLocation(muzzleSocket);
FRotator projectileRotation = (reticleTargetLocation - muzzleLocation).Rotation();