Time for action – controlling the turret
This script will allow the player to rotate their turret and aim the cannon.
The last script we need to create for our tank is
TurretControls
. This script will allow players to rotate the turret left and right and tilt the cannon up and down. As with all of the others, create it in theScripts
folder.The first two variables we define will hold pointers to the turret and cannon pivots, the empty
GameObjects
that we created for our tank. The second set is the speed that our turret and cannon will rotate at. Finally we have some limit values. If we didn't limit how much our cannon could rotate, it would just spin around and around, passing through our tank. This isn't the most realistic behavior for a tank, so we must put some limits on it. The limits are in the range of 300 because straight ahead is zero degree and down is 90 degree. We want it to be the upwards angle, so it is in the range of 300. We also use 359.9 because Unity will change 360 to zero...