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"

Projectile not firing in the right direction

I'm making a top down kinda isometric game and I'm trying to have the player fire projectiles. I'm making the player and the gun separate actors. The player class attaches the gun to a bone socket and then tells it when to fire.The issue is that it's not firing in the direction the player is facing. Here is the firing code:

 UWorld* const World = GetWorld();
         
     if (bFiring && FireCoolDownTimer < World->GetTimeSeconds())
         {
             if (World)
             {
 
                 if (ProjectileClass != NULL)
                 {
                     // Get the positions transforms
                     // Get the camera transform
                     FVector Loc = GetActorLocation();
                     FRotator Rot = GetAttachParentActor()->GetActorRotation();
                     FVector const MuzzleLocation = Loc + MuzzleOffset;  
 
 
 
                     FActorSpawnParameters SpawnParams;
                     SpawnParams.Owner = this;
                     SpawnParams.Instigator = Instigator;
                     // spawn the projectile at the muzzle
                     AProjectile* const Projectile = World->SpawnActor<AProjectile>(ProjectileClass, MuzzleLocation, Rot, SpawnParams);
                     if (Projectile)
                     {
                         FVector const LaunchDir = Rot.Vector();
                         Projectile->InitVelocity(LaunchDir);
                     }
 
                 }//projectile check end
             
         }//world check end
             FireCoolDownTimer = World->GetTimeSeconds() + FireCoolDown;
     }//bfiring check end


The "InitVelocity" code is:

 void AProjectile::InitVelocity(const FVector& ShootDirection)
 {
     if (ProjectileMovement)
     {
         // set the projectile's velocity to the desired direction
         ProjectileMovement->Velocity = ShootDirection * ProjectileMovement->InitialSpeed;
     }
 }

Product Version: Not Selected
Tags:
more ▼

asked Sep 20 '14 at 03:02 PM in C++ Programming

avatar image

Namr2000
1 1 2 4

avatar image RuBa1987 Sep 26 '14 at 12:16 PM

What object is the attached parent?

You should be firing along the local x axis. I dont think you want to shoot relative to the world.

Make sure that the x axis of the parent lines up with x axis of the gun.

Something else you could try that's a bit of a hack is you can calculate the look at rotation between your projectile and the character (use this UKismetMathLibrary::FindLookAtRotation) and subtract 180 on the x.

avatar image Namr2000 Sep 27 '14 at 12:16 AM

I have made it so that this code now executes inside of the Player class I have also changed this line to fire across the X axis but it is still firing on the world space X axis

FVector const LaunchDir = FTransform(Rotation).GetScaledAxis(EAxis::X);

Projectile->InitVelocity(LaunchDir);

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

4 answers: sort voted first

I think getting the forward vector might be what you are looking for.

more ▼

answered Sep 20 '14 at 04:36 PM

avatar image

dzeligman
708 32 28 53

avatar image Namr2000 Sep 21 '14 at 07:19 PM

That didn't help I still have the same result of the projectile being fired in the same direction

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

check your BP/projectile class and make sure 'Projectile Movement.Initial velocity in local space' property is false.

more ▼

answered Sep 23 '14 at 11:56 AM

avatar image

evilrat666
1 1 1

avatar image Namr2000 Sep 25 '14 at 12:28 AM

Turning that on seems to have no effect as well.

avatar image evilrat666 Sep 25 '14 at 08:22 AM

It should be turned off, and looks like you also need normalize vector you get from FRotator.

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

I know that you have to have the rotation set to world instead of local. Our programmer was having the exact same problem.

more ▼

answered Sep 25 '14 at 12:51 AM

avatar image

TSpartanT
116 17 20 26

avatar image TSpartanT Sep 25 '14 at 12:52 AM

For some reason having local rotation makes it go in random directions.

avatar image Namr2000 Sep 25 '14 at 01:58 PM

How do you transform a rotation vector into world space?

avatar image TSpartanT Sep 26 '14 at 05:56 PM

I have no idea how he did it.. I just know that this fixed his problem.

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

http://pastebin.com/c3H5XVia

I fixed the issue, the tutorial is retarded or something...

more ▼

answered Oct 27 '15 at 11:57 PM

avatar image

Strakh
0

(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