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"

C++ How to fire projectile at mouse cursor in Top Down Template

I'm trying to get my character to shoot a projectile at where I click with my cursor in C++. Im using the top down template.

If I could get the position for the mouse click I could create a Vector from the player to the position and add this as velocity to the projectile.

alt text

The projectile flys towards the camera when I click in the position of the blue mouse click.

Function code.

 void ARaiderCharacter::OnFire()
 {
     // Get the coordinates of the mouse from our controller  
     float LocationX;
     float LocationY;
     APlayerController* PlayerController = Cast<APlayerController>(GetController());
     PlayerController->GetMousePosition(LocationX, LocationY);
     FVector2D MousePosition(LocationX, LocationY);
 
 
     FVector LaunchDir;
 
     FHitResult Hit;
     Controller->CastToPlayerController()->GetHitResultUnderCursor(ECC_Visibility, false, Hit);
 
     if (Hit.bBlockingHit){
         if (Hit.Actor != NULL){
             
             LaunchDir = Hit.ImpactPoint;
         }
     }
 
     // try and fire a projectile
     if (ProjectileClass != NULL)
     {
         // Get the camera transform
         FVector CameraLoc;
         FRotator CameraRot;
         GetActorEyesViewPoint(CameraLoc, CameraRot);
         // MuzzleOffset is in camera space, so transform it to world space before offsetting from the camera to find the final muzzle position
         FVector const MuzzleLocation = CameraLoc + FTransform(CameraRot).TransformVector(MuzzleOffset);
         FRotator MuzzleRotation = CameraRot;
         MuzzleRotation.Pitch += 0.0f;          // skew the aim upwards a bit
         UWorld* const World = GetWorld();
         if (World)
         {
             FActorSpawnParameters SpawnParams;
             SpawnParams.Owner = this;
             SpawnParams.Instigator = Instigator;
             // spawn the projectile at the muzzle
             AProjectile* const Projectile = World->SpawnActor<AProjectile>(ProjectileClass, MuzzleLocation, MuzzleRotation, SpawnParams);
             if (Projectile)
             {
                 // find launch direction
                 FVector const LaunchDir = Hit.ImpactPoint;
                 //Projectile->InitVelocity(LaunchDir);
                 Projectile->InitVelocity(LaunchDir);
             }
         }
     }
 }


Product Version: UE 4.9
Tags:
more ▼

asked Nov 17 '15 at 10:04 PM in C++ Programming

avatar image

damorton
26 2 6 6

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

1 answer: sort voted first

This is how I fixed my problem.

 void ARaiderCharacter::OnFire()
 {
     // Get the coordinates of the mouse from our controller  
     float LocationX;
     float LocationY;
     APlayerController* PlayerController = Cast<APlayerController>(GetController());
     PlayerController->GetMousePosition(LocationX, LocationY);
     FVector2D MousePosition(LocationX, LocationY);
 
 
     FVector LaunchDir;
 
     FHitResult Hit;
     //Controller->CastToPlayerController()->GetHitResultUnderCursor(ECC_Visibility, false, Hit);
     Controller->CastToPlayerController()->GetHitResultUnderCursor(ECC_Visibility, false, Hit);
 
     if (Hit.bBlockingHit){
         if (Hit.Actor != NULL){
             
             LaunchDir = Hit.ImpactPoint;
         }
     }
 
     // try and fire a projectile
     if (ProjectileClass != NULL)
     {
         // Get the camera transform
         FVector CameraLoc;
         FRotator CameraRot;
         GetActorEyesViewPoint(CameraLoc, CameraRot);
         // MuzzleOffset is in camera space, so transform it to world space before offsetting from the camera to find the final muzzle position
         FVector const MuzzleLocation = CameraLoc + FTransform(CameraRot).TransformVector(MuzzleOffset);
         FRotator MuzzleRotation = CameraRot;
         MuzzleRotation.Pitch += 0.0f;          // skew the aim upwards a bit
         UWorld* const World = GetWorld();
         if (World)
         {
             FActorSpawnParameters SpawnParams;
             SpawnParams.Owner = this;
             SpawnParams.Instigator = Instigator;
             // spawn the projectile at the muzzle
             AProjectile* const Projectile = World->SpawnActor<AProjectile>(ProjectileClass, MuzzleLocation, MuzzleRotation, SpawnParams);
             if (Projectile)
             {
                 // find launch direction
                 FVector const LaunchDir = (Hit.ImpactPoint - MuzzleLocation).GetSafeNormal();
                 //Projectile->InitVelocity(LaunchDir);
                 Projectile->InitVelocity(LaunchDir);
             }
         }
     }
 }
more ▼

answered Nov 28 '15 at 11:01 PM

avatar image

damorton
26 2 6 6

(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