How can I have a bullet travel a constant distance in the direction of the cursor?

Hi everyone,

Maybe I’m not understanding well how projectiles work in UE4, but I have a problem with them. I’m making a simple 2D game in which the player can shoot in the direction the mouse marks.

EDIT: I’m working with the 2D platformer preset added in Unreal Engine 4.3, with paper2D plugin.

I’ve done a simple projectile (bullet) shot by the player towards the mouse which is working fine. Here is the blueprint for this:

I have also set an editable, exposed in spawn variable in my bullet blueprint:

Now, the problem is when I shoot with my character, the bullet will travel to where the mouse is. If the mouse is near the player, it will stay there until it expires. What I want is the bullet to travel a constant distance in the direction the mouse is. If the mouse is near the player, I want the bullet to go past it.

I thought that by having a unitarian direction vector (bottom part of first blueprint) and multiplying it by some number (distance I want it to go) and setting this as the velocity, it would work. Obviously I’m wrong.

What am I missing?

Thank you.

Hi, thank you for your response!

I’m not doing a top/down level. I’m currently testing things over the 2D platformer preset on Unreal Engine 4.3 with paper2D plugin.

Also, it is not a problem with collision with BSP as I’ve put none yet.

I’ll elaborate a little bit more on the problem:

When I shoot, there is a projectile that gets spawned on player character position and travels a certain distance. This distance, right now, is the distance to the mouse cursor position. If the mouse is at 100 units from the player, the bullet travels those 100 units and no more. It ends it’s movement at the cursor position. If it is at 500 units, it travels those 500.

The lifespan of those projectiles is 2 seconds. That means if projectile takes 0.5 seconds to get to the mouse, it will wait 1.5 seconds over the position of the mouse and then disappear.

What I want is the bullet to keep moving during it’s lifespan (which right now it’s 1.5 seconds) at max speed. If it gets to the mouse in 0.5 it should no stay quiet there, but keep moving in the same direction for the remaining 1.5 seconds.

I hope I made it a little clearer now.

However reading your post again, maybe
what you want to do is change the
‘Convert Mouse Location to world
space’ node`s output to be the ‘World
Direction’ instead of ‘Location’.
(This may be wrong so sorry i am
unsure.)

I’ve tried this already with no success. Apparently, direction is always x=0, y = -1, z = 0.

Before you calculate the direction and rotation vectors for your bullet, try changing the Y coordinate of the mouse position you get to the Y coordinate of the character position:

Based on your previous comment that the world direction of a mouse click is “x=0, y = -1, z = 0” it seems like you would want your projectile to move only along the X Z plane.

I cannot thank you enough for this. I’ve been looking into it and didn’t think about this tiny detail.

It’s logic as the Y coordinate of the mouse doesn’t need to be the same as the character. The bullet was behaving correctly but was being shot in 3D instead of the plane it was intended to be.

Thank you again!