Tank Game Basics! How to build, animate and control a tank
Tutorial, Unity
0
Create a small tank game. Learn how to build, animate and control 2D tanks using free assets. Full C# script included.
❤️ Subscribe to Oxmond Tutorials:
https://bit.ly/SubscribeOxmondTutorials
✅ Download the free Tank Constructor tool here:
https://assetstore.unity.com/packages/2d/textures-materials/tank-constructor-131316?aid=1100l4p9k
✅ Other free Asset Packages at the Unity Assets Store:
https://assetstore.unity.com/lists/top-free-packages-13201?aid=1100l4p9k
😷👕 Need a face mask / developer T-shirt? Drop by our shop and get a 20% DISCOUNT on your first purchase by using the discount code OXMONDSALE. Click here:
https://shop.oxmond.com/discount/OXMONDSALE
Tank script:
/* .-------. .--. .-------. .--. .--. .--. | |--.--.--------.-----.-----.--| | |_ _|--.--| |_.-----.----|__|---.-| |-----. | - |_ _| | _ | | _ | | | | | | _| _ | _| | _ | |__ --| |_______|__.__|__|__|__|_____|__|__|_____| |___| |_____|____|_____|__| |__|___._|__|_____| © OXMOND / www.oxmond.com */ using UnityEngine; public class Tank : MonoBehaviour { /////*******************************************///// ///// VARS ///// /////*******************************************///// public Track trackLeft; public Track trackRight; public string keyMoveForward; public string keyMoveReverse; public string keyRotateRight; public string keyRotateLeft; bool moveForward = false; bool moveReverse = false; float moveSpeed = 0f; float moveSpeedReverse = 0f; float moveAcceleration = 0.1f; float moveDeceleration = 0.20f; float moveSpeedMax = 2.5f; bool rotateRight = false; bool rotateLeft = false; float rotateSpeedRight = 0f; float rotateSpeedLeft = 0f; float rotateAcceleration = 4f; float rotateDeceleration = 10f; float rotateSpeedMax = 130f; /////*******************************************///// ///// UPDATE ///// /////*******************************************///// void Update() { rotateLeft = (Input.GetKeyDown(keyRotateLeft)) ? true : rotateLeft; rotateLeft = (Input.GetKeyUp(keyRotateLeft)) ? false : rotateLeft; if (rotateLeft) { rotateSpeedLeft = (rotateSpeedLeft < rotateSpeedMax) ? rotateSpeedLeft + rotateAcceleration : rotateSpeedMax; } else { rotateSpeedLeft = (rotateSpeedLeft > 0) ? rotateSpeedLeft - rotateDeceleration : 0; } transform.Rotate(0f, 0f, rotateSpeedLeft * Time.deltaTime); rotateRight = (Input.GetKeyDown(keyRotateRight)) ? true : rotateRight; rotateRight = (Input.GetKeyUp(keyRotateRight)) ? false : rotateRight; if (rotateRight) { rotateSpeedRight = (rotateSpeedRight < rotateSpeedMax) ? rotateSpeedRight + rotateAcceleration : rotateSpeedMax; } else { rotateSpeedRight = (rotateSpeedRight > 0) ? rotateSpeedRight - rotateDeceleration : 0; } transform.Rotate(0f, 0f, rotateSpeedRight * Time.deltaTime * -1f); moveForward = (Input.GetKeyDown(keyMoveForward)) ? true : moveForward; moveForward = (Input.GetKeyUp(keyMoveForward)) ? false : moveForward; if (moveForward) { moveSpeed = (moveSpeed < moveSpeedMax) ? moveSpeed + moveAcceleration : moveSpeedMax; } else { moveSpeed = (moveSpeed > 0) ? moveSpeed - moveDeceleration : 0; } transform.Translate(0f, moveSpeed * Time.deltaTime, 0f); moveReverse = (Input.GetKeyDown(keyMoveReverse)) ? true : moveReverse; moveReverse = (Input.GetKeyUp(keyMoveReverse)) ? false : moveReverse; if (moveReverse) { moveSpeedReverse = (moveSpeedReverse < moveSpeedMax) ? moveSpeedReverse + moveAcceleration : moveSpeedMax; } else { moveSpeedReverse = (moveSpeedReverse > 0) ? moveSpeedReverse - moveDeceleration : 0; } transform.Translate(0f, moveSpeedReverse * Time.deltaTime * -1f, 0f); if (moveForward | moveReverse | rotateRight | rotateLeft) { trackStart(); } else { trackStop(); } } /////*******************************************///// ///// METHODS ///// /////*******************************************///// void trackStart() { trackLeft.animator.SetBool("isMoving", true); trackRight.animator.SetBool("isMoving", true); } void trackStop() { trackLeft.animator.SetBool("isMoving", false); trackRight.animator.SetBool("isMoving", false); } }
Tracks script:
/* .-------. .--. .-------. .--. .--. .--. | |--.--.--------.-----.-----.--| | |_ _|--.--| |_.-----.----|__|---.-| |-----. | - |_ _| | _ | | _ | | | | | | _| _ | _| | _ | |__ --| |_______|__.__|__|__|__|_____|__|__|_____| |___| |_____|____|_____|__| |__|___._|__|_____| © OXMOND / www.oxmond.com */ using System.Collections; using System.Collections.Generic; using UnityEngine; public class Track : MonoBehaviour { public Animator animator; }