How do I select an angle to fire a projectile?

Okay so basically, a game is being created in C++ in unreal. The game is 2D and the character should be able to fire projectiles. Basically, i’m fairly new to unreal and require a push in the right direction to getting this implemented.

I want to be able to do two different things:

  1. How would I limit the the mouse cursor to only be allowed on the red line, such that if you move the mouse upwards it will go to one end of the red line, or down and it will go down another?

  2. How would I calculate the angle between the character and the mouse position, to calculate the angle to fire the projectile off in?

I’m not asking for a full solution, i’m just wanting a push in the right direction?

Any help would be greatly appreciated! (Sorry about the bad paint drawing, haha)

Hi @ibaconbuttys ,

  1. I’m not entirely sure how one can limit the mouse movement entirely what you can do is actually track the direction of the mouse movement using its position you can get this by using PlayerController->GetMousePositon(X,Y). You could then use it figure out direction you mouse is moving. by using the last know position Once the direction is know you could move the retiicle up or down by using the formula X=Cos(Angle)*radius,Y = Sin(Angle) * radius.
  2. You would always have a set Angle before hand. What you’d actually need is the is the direction which the cone/the character is facing/ wants to face.

You can also use vectors in 2D. Then the angle is:

acos( dot( PawnViewDirection / |PawnViewDirection|, ProjectileDirection / |ProjectileDirection| )).

Use this angle to limit the movement in your PlayerController class.