x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Office Holiday

Epic Games' offices will be on holiday from June 22nd to July 7th. During this period support will be limited. Our offices will reopen on Monday, July 8th. 

Shooting in the direction of the crosshair

The problem is in concept very simple. I have a Crosshair that is displayed slightly below the center of the screen. I already looked at Adjusting aim direction to crosshair position but the projectiles always go toward the top left portion of the screen. Now is the tricky part. How to make trace, so it will end exactly at the crosshair position ?

So far I have been using this:

For the HUD location

 void ABaseHUD::Get3DCrossHairPos()
 {
     //Always Check Pointers
     if (!Canvas) return;
 
     FVector WorldDirectionOfCrossHair2D;
     Canvas->Deproject(CrossHairCenter, CrossHair3DPos, WorldDirectionOfCrossHair2D);
     PrintDebug(555, FColor::Silver, FString::Printf(TEXT("Crosshair 3D pos: %f %f %f"), CrossHair3DPos.X, CrossHair3DPos.Y, CrossHair3DPos.Z));
     //moves the cursor pos into full 3D, your chosen distance ahead of player
     CrossHair3DPos += WorldDirectionOfCrossHair2D * AIM_DISTANCE;
     PrintDebug(663, FColor::Silver, FString::Printf(TEXT("Crosshair Center: %f %f"), CrossHairCenter.X, CrossHairCenter.Y));
     
 }
 
 void ABaseHUD::DrawHUD()
 {
     Super::DrawHUD();
 
     if (CrosshairTexture)
     {
         //Find the center of our canvas.
         FVector2D Center(Canvas->ClipX * 0.5f, Canvas->ClipY * 0.55f);
 
         //Offset by half of the texture's dimensions so that the center
         //of the texture aligns with the center of the canvas.
         FVector2D CrossHairDrawPosition(Center.X - (CrosshairTexture->GetSurfaceWidth() * 0.5f), Center.Y - (CrosshairTexture->GetSurfaceHeight() * 0.55f));
 
         //Draw the crosshair at the centerpoint.
         FCanvasTileItem TileItem(CrossHairDrawPosition, CrosshairTexture->Resource, FLinearColor::White);
         TileItem.BlendMode = SE_BLEND_Translucent;
         Canvas->DrawItem(TileItem);
 
         Get3DCrossHairPos();
     }
 }

and for the line trace

 void ABaseWeapon::ShootProjectile()
 {
  ..........................................
  ..........................................
 UCameraComponent* const OwnerCamera = MyPawn->GetFirstPersonCameraComponent();
             
             // MuzzleOffset is in camera space, so transform it to world space before offsetting from the character location to find the final muzzle position
             const FVector SpawnLocation = FP_MuzzleLocation->GetComponentLocation();
 
             FVector StartPos = OwnerCamera->GetComponentLocation();
 
             FVector ForwardVector = OwnerCamera->GetForwardVector();
             ABaseHUD* HUD = Cast <ABaseHUD>(Cast <APlayerController>(MyPawn->GetController())->GetHUD());
             ForwardVector = StartPos + (ForwardVector * 20000);
             const FVector End = HUD ? HUD->CrossHair3DPos : (StartPos + (ForwardVector * 20000));
             const FHitResult TargetPos = LineTrace(StartPos, End);
             
             if (TargetPos.bBlockingHit)
             {
                 PrintDebug(363, FColor::Silver, TEXT("There was a hit"));
                 //Set Spawn Collision Handling Override
                 FActorSpawnParameters ActorSpawnParams;
 
                 FRotator SpawnRotation2 = UKismetMathLibrary::FindLookAtRotation(SpawnLocation, TargetPos.ImpactPoint);
                 PrintDebug(398, FColor::Silver, FString::Printf(TEXT("Camera Forward: %f %f %f"), ForwardVector.X, ForwardVector.Y, ForwardVector.Z));
                 PrintDebug(399, FColor::Silver, FString::Printf(TEXT("Ray Hit pos: %f %f %f"), TargetPos.ImpactPoint.X, TargetPos.ImpactPoint.Y, TargetPos.ImpactPoint.Z));
                 World->SpawnActor<ABaseProjectile>(ProjectileClass, SpawnLocation, SpawnRotation2, ActorSpawnParams);
             }
             else
             {
                 FActorSpawnParameters ActorSpawnParams;
                 World->SpawnActor<ABaseProjectile>(ProjectileClass, SpawnLocation, UKismetMathLibrary::FindLookAtRotation(SpawnLocation, End), ActorSpawnParams);
             }
  ..........................................
 }

I have been trying for several hours now without any success, any help/suggestion is welcome.

Product Version: UE 4.18
Tags:
more ▼

asked Jan 22 '18 at 05:03 AM in C++ Programming

avatar image

NotNullptr
1 1 2 4

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

2 answers: sort voted first

It did not work because I did not set CrossHairCenter = Center in the DrawHUD method.

more ▼

answered Jan 24 '18 at 09:39 PM

avatar image

NotNullptr
1 1 2 4

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

Hey there, you don't really need to deproject, if the crosshair is centered you can just get the camera location and forward vector. About the question you asked, do you really need to have a projectile spawned, a line trace is usually better for this. If you still want the projectile then you need to set the gravity scale to 0 so it doesn't affect the projectile. For the projectile spawn rotation, just set it to the Rotation of the world forward vector of the camera.

more ▼

answered Jan 22 '18 at 10:52 AM

avatar image

xlar8or
6k 161 129 304

avatar image NotNullptr Jan 22 '18 at 03:05 PM

You are absolutely right, at first I used a centered crosshair and did what you suggested which worked great but I wanted to have my crosshair slightly below the center of the screen but I could not make it work.

For reference, what I wanted to achieve is this (Halo 3: ODST): alt text

What I got instead is this: alt text

Disregard the "Crosshair 3D pos", it is actually the crosshair positon multiplied by the range of the weapon.

jz9ctkf.jpg (204.9 kB)
info.png (746.9 kB)
avatar image xlar8or Jan 22 '18 at 03:10 PM

In that case it might be easier to use deproject.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question