Download

projetctile needs to fly to the center of the screen!!

I got a very simple shooter game so far. but I am facing one issue. I got a gun, and i am spawning a projectile from a muzzle of it and the projectile flys straight. But what is want is, to spawn from a muzzle and send a projectile to the centre of the screen (Crosshair) as usual Shooter games do.Hope you understood my issue, How can I handle this?

I have the same problem and not yet solved. Let’s stay in contact when one of us has solved it.

We have a player state that line traces through screen center. Code snippets for finding center and sending a trace through:



// get current camera location and rotation
FVector cameraLocation = _playerController->PlayerCameraManager->GetCameraLocation();
FRotator cameraRotation = _playerController->PlayerCameraManager->GetCameraRotation();
FVector cameraDirection = cameraRotation.Vector().GetSafeNormal();

// calculate ray start point, ray end point, hit result, collision parameters
// ray start point is the camera location
// ray end point is a ray cast through the middle of the screen
// collision query parameters determined by player controller (mainly to ignore hitting certain objects)
FVector traceStartLocation = cameraLocation;
FVector traceEndLocation = cameraLocation + MAXIMUM_INTERACTION_DISTANCE * cameraDirection;
FHitResult traceHitResult(ForceInit);
FCollisionQueryParams traceParameters = _playerController->GetCollisionQueryParameters();

// do ray trace
_playerController->GetWorld()->LineTraceSingleByChannel(traceHitResult, // result
traceStartLocation, // start
traceEndLocation, // end
ECC_Pawn, // collision channel
traceParameters); // trace parameters


We need a projectile that spawns, not a line trace.

Dude i have solved this issue , but it might be little confusing, I’ll try my best)
so:
I got a projectile.cpp class which contains UE4 engine’s component called “Projectile Movement component”
and has a custom function called “FireInDirection” which is responsible for firing bullet to the appropriate direction
Here is “ProjectileMovementComponent->Velocity” is responsible for both Direction and Speed

[FONT=Courier New]void AProjectile::FireInDirection(const FVector& ShootDirection)
{
//gives a velocity to the projectile with appropirate Shot Direction
ProjectileMovementComponent->Velocity = ShootDirection * ProjectileMovementComponent->InitialSpeed;
}

and then i got a gun.cpp class which has a function “SpawnProjectile” where you need to get a Player’s viewpoint which sets a current camera location and rotation .
then you need to use “CameraRotation.Vector()” which fives a center of the screen . and then you simply call “projectile.cpp” function LaunchInDirection() and pass value Center of the screen
and bullet will fire to the center of the screeen

[FONT=Courier New]void AGun::SpawnProjectile()
{
if (ProjectileClass)
{
FVector CameraLocation;
FRotator CameraRotation;
GetOwnerController()->GetPlayerViewPoint(CameraLocation, CameraRotation);

FVector LocationToSpawn = ProjectileSpawnPoint->GetComponentLocation();
FRotator RotationToSpawn = ProjectileSpawnPoint->GetComponentRotation();

TempProjectile = GetWorld()->SpawnActor<AProjectile>(ProjectileClass, LocationToSpawn, RotationToSpawn/Rotation of SM/);
TempProjectile->SetLifeSpan(2.f);//projectile will be destroyed after 2 secs after spawning
if (TempProjectile)
{

FVector LaunchDirection = CameraRotation.Vector();//gives a ceneter of the screen
TempProjectile->FireInDirection(LaunchDirection);//wiill send a projjectile to the center of the screen

}

}

}

Hope that will be helpful , I know that its a bit confusing explanation ))) I have tried all my best, or you can contact me to the e-mail:aibar.ulanov@gmail.com for may be better explanation, but i don’t promise )))

wonderful ulanov, thank you very much for your effort, very big thank you. I will try the days to understand it.

So, in blueprints I have found the solution, see photo. I would need that now in C++. Does one of you dare to do it, doesn’t look that hard?

I tried "BP on “File”–>“Developer”–>“generate Native Code”, but it looks so confusing, no chance to understand it.