Follow mouse at spawn projectile debug

Hello everyone,

I am relatively new to UE4. I made this Follow mouse at cursor section. But theres something wrong with my rotation because the spawned object has a different rotation everytime. Please help me out.

Here’s my progress so far. Still stuck.
I want the bullets to be shot towards the position of my mouse on the screen.
I get a pretty much random behaviour of the spawn objects. They either get spawned on the back of my character and go in an inverse direction of the desired one or I can’t see them at all.
If anyone knows a better way to do this, please help me out.
This is my view space.
screenshots attached

Hey there, from your gameplay image I can’t really tell where your cursor is or the problem in action.
But from the other info, let’s see.

First: It seems you are getting your Player’s position and adding it to your camera’s position and using that to find the starting rotation of the bullet.
If the actual character on screen is the one shooting the gun, you shouldn’t use the camera position to determine the shoot direction.

To find the look rotation that the bullet will use, you should set the location of the raycast hit as the target and the actor location of the player as the starting point.
Don’t use the camera’s position.

This will make the bullet have the direction from the center of the character’s body to the point of impact of the raycast.
However, if you clicked on a surface that is higher/lower than the player, the bullet will move up and down.

If you don’t want the bullets to move up/down, you will have to break the resulting rotation and set the Roll or Pitch to 0, I don’t remember which right now, before making the transform.

Another thing, sometimes the bullet goes in a very weird direction because you are not testing if the raycast you did hit any object.
If the raycast didn’t hit any object, the location of the hit will be 0,0,0, making the bullet go in the direction of the world origin, that is probably why sometimes it shoots behind your character.

What you should do is have an If that checks if blocking hit is true.
If the blocking hit is true, you use the logic you already have.

If the blocking hit is false, you can make the character not shoot OR shoot in the direction that the player is facing.
To shoot in the direction that the player is facing, you can just get the actor rotation of your player and use that as the rotation of the transform for the new bullet.

In the future, you will have to set up in a way that you make the bullet spawn from the gun (or wherever it will originate instead of your character’s center), but then you just replace the actor location for the actual bullet spawn location. This got long so I’ll leave you at this for the moment, but it should fix most of your problems.