How to enable aim with array of cannons

So I have been working on a project wich includes a ship. And on that ship I want to be able to aim and shoot with cannons. But I have not been able to figure out how the best approach is. And also I don’t know what is easier to do because now I have the cannon as a seperate bp pawn wich if I press a button it fires but I cannot aim when I press right mouse button. (Also I need to figure out how when I look at the right side of the boat my camera goes “in aiming mode” on the right and the same I want on the left but I will need to figure that out after that).

If anyone knows how please help me.

Thanks in advance!

Hey Core21,

Thanks for your quick and thorough awnser! My cannons and ship are staticl meshes (Sails not since they need to be affected by wind). Although I consider myself to have some experience in Unreal Engine I do not quite get how you set your proposition into action. Could you give me an example on how to set it up? Me and some friends are still working on other ships so since you would handle them as skeletal meshes would you think it is wise to change this one to skeletal mesh and put the cannons in when creating the meshes?

Thanks in advance!

Hello SchoolRenders,

Could you give me more details about your ship and cannon meshes? Are they static meshes or skeletal meshes?

As far as I understand, your cannons are static meshes, and you attach them to the ship using child actors. To rotate your cannons, you need a variable. Let’s say “Target” (vector) to save where the player is aiming at. When the player aims, a line trace function will be fired. Target variable will be the hit point which is a result from the line trace. Then you need to use the “Find Look at Rotation” node to get rotation values for each cannon. The results of those nodes will be the new rotations for the cannons. For example if you have 3 cannons, you need to use 3 Find Look at Rotation and give rotation values to each cannons separately.

If you asked my opinion, I would use this approach. It’s a bit similar. I create a skeletal mesh, and give a bone for each cannon. Let’s say the body of the ship has one bone named “body” and the cannons have “cannon1”, “cannon2” and so on. Then I create some variables to check if the player is aiming and where the player is aiming at. Let’s say the variables are “IsAiming” (boolean) and “Target” (vector). When the player aims, IsAiming variable will be true, and a line trace function will be fired. Target variable will be the hit point which is a result from the line trace. In the animation blueprint of the ship, every bone related to the cannons will be modified by the “LookAt” node. For example, if you have 3 cannons then you’ll need 3 LookAt nodes. Every LookAt node should be setup correctly, and “(As pin) Look at Location” should be checked. This will give you a pin to setup the location. The Target variable should be connected to this pin. By doing this, when the player aims, the cannons will be rotated and aim at the target. When the player doesn’t aim, IsAiming variable will be false and no longer need to fire a line trace function. Target variable will be zero vector. With this approach, your cannons will be turned immediately. To prevent this moment, you can use a interpolation function. This also leads another problem depending on what you want to achieve. By using a interpolation function, your aiming will be delayed. If you don’t want a delay in the aiming mode, you can use IsAiming variable. If the player aims, you can change the interpolation speed, which will give you a immediate turn visualization.

In my opinion, make the ships as skeletal meshes will be more good, however; there is no big difference between two approaches logically.

If you follow my approach, you need to create those two variables in the ship blueprint. In the blueprint, you need to create an input event. This is basically a normal event but this one has “Pressed” and “Released” pins. Released pin is easy. You need to set IsAiming variable to false and Target variable to zero vector. Pressed pin, you need to set IsAiming variable to true and call a line trace function. Here is the document page of the line trace function.

Start location will be your camera location. End location will be addition of two vectors. First vector is start location and second vector is calculation of the forward vector of the camera which is multiplied by a big number (9999999 for example) or a range value if your cannons have a range.

The return value of the function shows if the line trace hits something. If the result is false, which means the target is out of range or the target is too far away, then you need to set your Target variable to zero vector. If the result is true then you need to break the line trace result (Out Hit) and set Target variable to hit point. Here is the document page of a result of a line trace function.

After these steps you need to access those variables in the animation blueprint of the ship. If you look at the following video, he’s getting variables from a blueprint. In your case, you need to get Target variable. 3:45 - 6:30 is what you need.

The last step is using LookAt node. In the Anim Graph you can find LookAt node, and your target location will be the Target variable which you get from the blueprint

Thank you very much for your awnser and time, I think I got it now!

I just realized that I missed something. I said you can use interpolation functions to give smooth transition but I was wrong. You don’t need to use any interpolation functions, and you need to use “Blend Poses by bool” node in Anim Graph. The active value of the node will be the IsAiming variable. In the true pose, there will be LookAt nodes to rotate the cannons. In the false pose, there will be no LookAt nodes, which means your cannons will be rotated back to original poses. Also, the node has blend times. You can make smooth transitions by adjusting those numbers.