Tank with Turret and Cannon - Epic Wiki

# Tank with Turret and Cannon

From Epic Wiki

Jump to: navigation, search

Note This tutorial is a work in progress. There is a lot of content still being worked on.

# Contents

# Overview

A generic tank is built with treads, a hull, a turret and a cannon. In this tutorial we will be explaining how to create a complete simple tank that uses the mouse to swivel the turret and pitch the camera independent from the body's movement.

This tutorial is a pet project of the Unreal Engine Support team. We intend to give all of the basics of creating a tank, but as we go on we plan on adding additional functions that a tank would have.

# Initial Setup

1. First you will want to make sure that you have a couple of Input Axis created to make things simpler. You will need to make 4 in total:

InputAxis Tank.jpg

  • Move Forward - Used to move the Hull forward/backward.
  • Rotate Right - Used for the Hull's Yaw rotation. Right(D) returns positive Float values, Left(A) returns negative.
  • MouseRotateRight - Used for the Turret's Yaw rotation.
  • MouseLookUp - Used for the Cannon's Pitch rotation. Note that this is actually set to -1 instead of 1. This will actually make sure that the Pitch rotation is inverted, but if you like standard rotation (Moving the mouse forward tilts the cannon up), leave this positive.

2. Create a new Blueprint based on the Character Class, name it "MyTank" and open it up.

# Hull

This will be the body that is driven by the WASD controls. 'A' and 'D' will rotate the body in place, while 'W' and 'S' will move the tank back and forth based on the body's rotation.

# Component

First things first, you will need a few meshes to work with. For this part, all you need is a Hull (I am using a box mesh, but anything works). Since we are using a Character, the Hull will need to be a Skeletal Mesh, but it will have no bones, so import it as a Skeletal Mesh with Rigid Body checked on.

In the MyTank Blueprint, assign the Hull to the Mesh component's Skeletal Mesh. Adjust it into a position that you like.

Left

# EventGraph

# Hull Rotation

  1. Open the EventGraph
  2. Right-click and search for "InputAxis"
  3. To make this easier, click the small stars next to the different InputAxis Events to favorite them
  4. Add an InputAxis RotateRight node
  5. Drag off of the Axis Value and create a Make Rot
  6. Plug the Axis Value into the Yaw
  7. Add a Get for the Mesh component
  8. Drag off the Mesh node and create an Add Relative Rotation node
  9. Plug the RotateRight execution into the Add Relative Rotation
  10. Plug the Make Rot into the Delta Rotation

# Hull Movement

  1. Add an InputAxis MoveForward node
  2. Drag off execution pin and create a Add Movement Input node
  3. Plug the Axis Value pin into the Scale Value Input
  4. Drag off of the Mesh node and create a Get World Rotation node
  5. Drag off of Return Value on the Get World Rotation node and create a Break Rot node
  6. Drag off of Yaw output and create a Make Rot node
  7. Drag off of Return Value output and create a Get Forward Vector node
  8. Plug Return Value output into world direction of the Add Movement Input node

HullMovement Tank.jpg

# Turret

The turret rests on top of the Hull mesh, swiveling with the movement of the mouse. The camera will follow this movement to make sure the player can see where they are firing. It will also allow for the Hull to move in one direction while the Cannon fires in another.

# Component

The turret only rotates on the Yaw axis, that is its only function. Since this will be the best way of looking around, the camera should follow its movement.

  1. Add a Static Mesh Component parented under the Mesh component that holds the Hull
  2. Name this "Turret"
  3. Move the Turret to fit onto the Hull
  4. Add a Camera Component parented under the Turret
  5. In the Camera Component uncheck "Use Controller View Rotation"
  6. Move the Camera Component so that it is up and behind the entire tank

TurretAndCamComp Tank.jpg

# EventGraph

# Turret Rotation

  1. Add a InputAxis MouseRotateRight event node
  2. Add a Get for the Turret component
  3. Drag off the Get and add a Add Relative Roation node
  4. Connect the Execution from the MouseRotateRight into the Add Relative Rotation
  5. Drag off the MouseRotateRight node's Axis Value and create a Float * Float node
  6. Set the value to multiply by to 1.5(This merely increases the rate of rotation, as the unaltered delta value is quite small)
  7. Drag off the Float output of the multiplying node and create a Make Rot node
  8. Connect the output of the multiply into the Yaw of the Make Rot
  9. Connect the Return Value of the Make Rot into the Delta Rotation or the Add Relative Roation

TurretRotation Tank.jpg

# Cannon

The Cannon will Pitch up and down while rotating with the Turret's Yaw. It will use the mouse's movement, like the Turret, but only the Y-Axis movement. Unlike the Hull and Turret, the Cannon will only rotate between certain degrees so that it will not spin into the Hull and Turret.

# Component

This will be the simplest of the components:

  1. Add a Static Mesh Component parented under the Turret
  2. Name it "Cannon"
  3. Move it into position, so that it looks good with the Turret

CannonComp Tank.jpg

# EventGraph

  1. Create a Float Variable for holding the Cannons's Pitch rotation
  2. Name it "Cannon Rot Pitch"
  3. Add an InputAxis MouseLookUp event
  4. Drag from the Axis Value and create an Float + Float node
  5. Make a Get for the Cannon Rot Pitch
  6. Connect the Canon Rot Pitch into the Float + Float
  7. Drag from the adding node and create a Clamp (Float)
  8. Set the Clamp's Min to 0 and Max to 45
  9. Create a Set Cannon Rot Pitch
  10. Connect it to the MouseLoopUp's Execution
  11. Connect the Clamp's output to the Set Cannon Rot Pitch
  12. Create a Get for the Cannon Component
  13. Drag from the Get Cannon and create a Set Relative Rotation
  14. Connect the Set Cannon Rot Pitch to the Set Relative Rotation
  15. Drag from the Get Cannon and create a Get Relative Rotation
  16. Drag off Get Relative Rotation and create a Break Rot
  17. Drag off of the Break Rot and create a Make Rot
  18. Connect the Yaw and Roll values directly
  19. For the Pitch value, connect the Get Rot Pitch output
  20. Take the Return Value of the Make Rot and connect it with the Set Relative Rotation node's New Rotation input

CannonRotation Tank.jpg

MORE TO COME! STAY TUNED!

Retrieved from "https://wiki.unrealengine.com/index.php?title=Tank_with_Turret_and_Cannon&oldid=6471"

Categories: