Tank with Turret and Cannon - Epic Wiki
# Tank with Turret and Cannon
From Epic Wiki
Jump to: navigation, search
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:
- 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.
# EventGraph
# Hull Rotation
- Open the EventGraph
- Right-click and search for "InputAxis"
- To make this easier, click the small stars next to the different InputAxis Events to favorite them
- Add an InputAxis RotateRight node
- Drag off of the Axis Value and create a Make Rot
- Plug the Axis Value into the Yaw
- Add a Get for the Mesh component
- Drag off the Mesh node and create an Add Relative Rotation node
- Plug the RotateRight execution into the Add Relative Rotation
- Plug the Make Rot into the Delta Rotation
# Hull Movement
- Add an InputAxis MoveForward node
- Drag off execution pin and create a Add Movement Input node
- Plug the Axis Value pin into the Scale Value Input
- Drag off of the Mesh node and create a Get World Rotation node
- Drag off of Return Value on the Get World Rotation node and create a Break Rot node
- Drag off of Yaw output and create a Make Rot node
- Drag off of Return Value output and create a Get Forward Vector node
- Plug Return Value output into world direction of the Add Movement Input node
# 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.
- Add a Static Mesh Component parented under the Mesh component that holds the Hull
- Name this "Turret"
- Move the Turret to fit onto the Hull
- Add a Camera Component parented under the Turret
- In the Camera Component uncheck "Use Controller View Rotation"
- Move the Camera Component so that it is up and behind the entire tank
# EventGraph
# Turret Rotation
- Add a InputAxis MouseRotateRight event node
- Add a Get for the Turret component
- Drag off the Get and add a Add Relative Roation node
- Connect the Execution from the MouseRotateRight into the Add Relative Rotation
- Drag off the MouseRotateRight node's Axis Value and create a Float * Float node
- Set the value to multiply by to 1.5(This merely increases the rate of rotation, as the unaltered delta value is quite small)
- Drag off the Float output of the multiplying node and create a Make Rot node
- Connect the output of the multiply into the Yaw of the Make Rot
- Connect the Return Value of the Make Rot into the Delta Rotation or the Add Relative Roation
# 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:
- Add a Static Mesh Component parented under the Turret
- Name it "Cannon"
- Move it into position, so that it looks good with the Turret
# EventGraph
- Create a Float Variable for holding the Cannons's Pitch rotation
- Name it "Cannon Rot Pitch"
- Add an InputAxis MouseLookUp event
- Drag from the Axis Value and create an Float + Float node
- Make a Get for the Cannon Rot Pitch
- Connect the Canon Rot Pitch into the Float + Float
- Drag from the adding node and create a Clamp (Float)
- Set the Clamp's Min to 0 and Max to 45
- Create a Set Cannon Rot Pitch
- Connect it to the MouseLoopUp's Execution
- Connect the Clamp's output to the Set Cannon Rot Pitch
- Create a Get for the Cannon Component
- Drag from the Get Cannon and create a Set Relative Rotation
- Connect the Set Cannon Rot Pitch to the Set Relative Rotation
- Drag from the Get Cannon and create a Get Relative Rotation
- Drag off Get Relative Rotation and create a Break Rot
- Drag off of the Break Rot and create a Make Rot
- Connect the Yaw and Roll values directly
- For the Pitch value, connect the Get Rot Pitch output
- Take the Return Value of the Make Rot and connect it with the Set Relative Rotation node's New Rotation input
MORE TO COME! STAY TUNED!
Retrieved from "https://wiki.unrealengine.com/index.php?title=Tank_with_Turret_and_Cannon&oldid=6471"