Need help shooting projectiles at the center of the screen.

Edit: I forgot to mention that from what I read, the best way to go about it was to do a LineTrace where the camera is pointing at, normalize the impact point, and that should be the value for the muzzle if you want to shoot more towards the center.

Hello, I am currently trying to get my projectiles straight when aiming. After looking at other answers, I tried to do what others have said but still comes out off regardless if the character is crouching, looking up, or if the camera has switched sides.

Right now, my code looks like this (Muzzle is where the bullets are actually spawned at in my c++ file):

Which yields results like this (And even more off when crouching or with the camera switched:

Can anyone give me any pointers on what I am doing wrong or if I am missing something? Any Help will be appreciated.

Hello ,

I’m not quite I understand how you’re using the Muzzle variable. You say that you’re using the Muzzle as the spawn point for the projectiles but in the second screenshot it also seems like you’re setting Muzzle to be where the bullet ends up. Also, could you provide the code file (you can just upload the file itself instead of snippets) where the spawning occurs?

Hi,
What about maxRange?

It’s possible I may be using muzzle wrong. And I have been working on this for the past two hours, so the code keeps changing and it originally didn’t look like this. This was just after of two hours of trying different combinations to get it to work.

And it’s not letting me upload the C++ file, but here is the code:

void ABaseWeapon::FireWeapon()
{
	if (bCanFire == true)
	{

		Heat += (10 - HeatSupression); 

		if (Heat >= 100)
			OverHeatCD();

		else
			CDWeapon();

		const FRotator SpawnRotation = GetActorRotation();
		const FVector SpawnLocation = GetActorLocation() + SpawnRotation.RotateVector(Muzzle);

		UWorld* const World = GetWorld();
		if (World != NULL)
		{
			GunMesh->PlayAnimation(FireAnim, false);
			World->SpawnActor<ABaseBullet>(Bullet, SpawnLocation, SpawnRotation);
		}

		//World->GetTimerManager().SetTimer(AutoTimer, this, &ABaseWeapon::FireWeapon, AutoTime, false); Temporaly set to false since automatic fire is controlled by blueprint.
	}

	else
		StopFiring();
}

Have you tried shooting, immediately pausing, and unpossessing the character to make sure that the bullet is coming from the correct spot? The reason I mention this is because your calculation for the Spawn Location. I would suggest using a Socket that you place at the end of the barrel for the gun and use that Socket’s position as a reference for spawning the bullet.

When is the line trace called in relation to the FireWeapon button? Is it before or after? This matters due to how Muzzle is being used/set and could be causing some issues.

I didn’t think about this before for some reason, but it could be the line trace itself that is hitting the wrong spot. Could you try changing the Draw Debug Type on the Line Trace node to Persistent? It’ll show a box where it’s hitting the wall so you can see if it is hitting the right spot.

Going into the weekend so won’t be able to respond until next week at this point, but I’ll be sure to keep looking into it.

Max range doesn’t effect the bullets trajectory or centerness. Only how far it’ll do the trace. Even still, it’s not centered regardless if I aim at something relatively close to the player.

Tried unpossessing, wasn’t leaving the right spot. I tried spawning from a socket, which while it does fix the problem of the character sometimes getting shot by his own gun, I still have the issue of having my aim off, I’m still trying to figure how to use the muzzle vector now after that calculation.

And after, The fire button is handled in the Player Characters BP, which then sends a message to the gun the player currently has. Once the gun receives the message, before it actually fires, there’s a blueprint function that does a line trace to whatever the camera is pointing at. Then it fires. It then does an Automatic fire loop that keeps calling the event Pull Trigger, repeating the process before it overheats.

Tried it before, was hitting the right spot. I’m think it might have something to do with camera angle that might be causing it to go off center. I remember seeing a piece of code before that does a line trace exactly at the center of the screen by getting the screen position, but now I can’t find it anymore.

This actually looks correct to me. Welcome to 3rd person cameras! It’s impossible to get bullets to line up with your reticle in every situation, at every distance. Even in a first person shooter, you’ll find that projectiles are usually fired from the camera location, not the weapon muzzle (though muzzle flash will obviously play on the muzzle).

Would it be possible to get a copy of the project or a sample that has the shooting logic set up inside of it to tinker with? It can be private if you wish by sending a link to it via a private message on our forums. Here is a link to my [profile][1].

Otherwise, I suggest setting up some print strings or log messages to show you all of the calculations that are going on to see where the unexpected result is coming from.

[1]:

We haven’t heard from you in a while, . Are you still having problems with lining this up? If so, would it be possible to provide a copy of the project so that I can take a look at the problem first hand? In the meantime, I’ll be marking this as resolved for tracking purposes.