Skip to main content

Creating Cannon and Camera Movement

Operating System

Creating Cannon

We will now create the object that will shoot the enemy and the structure. To do that first locates the sprite inside the Sprites directory.

  1. Drag the body sprite of the cannon to the Scene View.
    tut52
    After that drag another sprite as a child for the body to complete the cannon GameObject.
    tut53
  2. Next is to set the component to each of the GameObject.
    tut54
  3. After setting up the components add a GameObject to Cannon as a child. This will be the spawning point for the bullet. Set its components as shown below.
    tut55
  4. Then Create two GameObject as child for the Player GameObject. This will be the wheel for the cannon to be able to stand on the ground. Set it component as shown below.
    tut56
  5. Go to the Scripts folder and create a new folder inside of it called Player.
  6. Create a C# script called Cannon.
  7. Write these block of code inside the Cannon Class:
    1. <br />
    2. public GameObject cannonBullet;<br />
    3. public Transform cannonTip;<br />
    4. public Slider powerLevel;<br />
    5. public AudioClip cannonShot;<br />
    6. public int maxShot;<br />
    7. public bool readyToShoot;<br />
    8. public int shot;
    9. <p>private int minLevel, maxLevel, timeDelay;<br />
    10. private bool isMax, isCharging;<br />
    11. private AudioSource audioSource;<br />
    12. private float minRot, maxRot;<br />
    13. private Vector3 currentRotation, touchPos;<br />
    14. private int counter = 0;<br />
    15. private Vector2 direction = Vector2.zero;</p>
    16. <p> void Awake(){<br />
    17. audioSource = GetComponent<AudioSource> ();<br />
    18. }</p>
    19. <p> // Use this for initialization<br />
    20. void Start () {<br />
    21. readyToShoot = true;<br />
    22. shot = maxShot;<br />
    23. timeDelay = 50;<br />
    24. minLevel = 0;<br />
    25. maxLevel = 50;<br />
    26. powerLevel.maxValue = maxLevel;<br />
    27. powerLevel.value = minLevel;<br />
    28. maxRot = 20;<br />
    29. minRot = -25;<br />
    30. Vector3 currentRotation = transform.rotation.eulerAngles;<br />
    31. }</p>
    32. <p> // Update is called once per frame<br />
    33. void Update () {<br />
    34. if (GameplayController.instance.gameInProgress) {<br />
    35. if (readyToShoot) {<br />
    36. if(Application.platform == RuntimePlatform.Android){<br />
    37. TouchCannonShoot ();<br />
    38. }else if(Application.platform == RuntimePlatform.WindowsEditor){<br />
    39. CannonShoot ();<br />
    40. }</p>
    41. <p> }</p>
    42. <p> if(Application.platform == RuntimePlatform.Android){<br />
    43. TouchCannonMovement ();<br />
    44. }else if(Application.platform == RuntimePlatform.WindowsEditor){<br />
    45. CannonMovement ();<br />
    46. }</p>
    47. <p> }<br />
    48. }</p>
    49. <p> void TouchCannonShoot(){<br />
    50. if(Input.touchCount > 0){<br />
    51. Touch touch = Input.GetTouch (0);</p>
    52. <p> touchPos = Camera.main.ScreenToWorldPoint (touch.position);</p>
    53. <p> Vector2 touchRayHit = new Vector2 (touchPos.x, touchPos.y);</p>
    54. <p> RaycastHit2D hit = Physics2D.Raycast (touchRayHit , Vector2.zero);</p>
    55. <p> if(hit.collider != null){<br />
    56. if(hit.collider.CompareTag("Player")){<br />
    57. if(touch.phase == TouchPhase.Stationary){<br />
    58. if (shot != 0) {<br />
    59. UpdatePowerLevel ();<br />
    60. isCharging = true;<br />
    61. }<br />
    62. }else if(touch.phase == TouchPhase.Ended){<br />
    63. if (shot != 0) {<br />
    64. GameObject newCannonBullet = Instantiate (cannonBullet, cannonTip.position, Quaternion.identity) as GameObject;<br />
    65. newCannonBullet.transform.GetComponent<Rigidbody2D> ().AddForce (cannonTip.right * powerLevel.value, ForceMode2D.Impulse);<br />
    66. if (GameController.instance != null && MusicController.instance != null) {<br />
    67. if (GameController.instance.isMusicOn) {<br />
    68. audioSource.PlayOneShot (cannonShot);<br />
    69. }<br />
    70. }<br />
    71. shot--;<br />
    72. powerLevel.value = 0;<br />
    73. readyToShoot = false;<br />
    74. isCharging = false;<br />
    75. }<br />
    76. }<br />
    77. }<br />
    78. }</p>
    79. <p> /*if(touch.phase == TouchPhase.Stationary){<br />
    80.   if (shot != 0) {<br />
    81.   UpdatePowerLevel ();<br />
    82.   }<br />
    83.   }else if(touch.phase == TouchPhase.Ended){<br />
    84.   if (shot != 0) {<br />
    85.   GameObject newCannonBullet = Instantiate (cannonBullet, cannonTip.position, Quaternion.identity) as GameObject;<br />
    86.   newCannonBullet.transform.GetComponent<Rigidbody2D> ().AddForce (cannonTip.right * powerLevel.value, ForceMode2D.Impulse);<br />
    87.   if (GameController.instance != null && MusicController.instance != null) {<br />
    88.   if (GameController.instance.isMusicOn) {<br />
    89.   audioSource.PlayOneShot (cannonShot);<br />
    90.   }<br />
    91.   }<br />
    92.   shot--;<br />
    93.   powerLevel.value = 0;<br />
    94.   readyToShoot = false;<br />
    95.   }<br />
    96.   }*/</p>
    97. <p> }<br />
    98. }</p>
    99. <p> void CannonShoot(){<br />
    100. if (Input.GetKey (KeyCode.Space)) {<br />
    101. if(shot != 0){<br />
    102. UpdatePowerLevel ();<br />
    103. }<br />
    104. }else if(Input.GetKeyUp(KeyCode.Space)){<br />
    105. if (shot != 0) {<br />
    106. GameObject newCannonBullet = Instantiate (cannonBullet, cannonTip.position, Quaternion.identity) as GameObject;<br />
    107. newCannonBullet.transform.GetComponent<Rigidbody2D> ().AddForce (cannonTip.right * powerLevel.value, ForceMode2D.Impulse);<br />
    108. if (GameController.instance != null && MusicController.instance != null) {<br />
    109. if (GameController.instance.isMusicOn) {<br />
    110. audioSource.PlayOneShot (cannonShot);<br />
    111. }<br />
    112. }<br />
    113. shot--;<br />
    114. powerLevel.value = 0;<br />
    115. readyToShoot = false;<br />
    116. }<br />
    117. }</p>
    118. <p> }</p>
    119. <p> void UpdatePowerLevel(){<br />
    120. if (!isMax) {<br />
    121. powerLevel.value += timeDelay * Time.deltaTime;</p>
    122. <p> if(powerLevel.value == maxLevel){<br />
    123. isMax = true;<br />
    124. }</p>
    125. <p> } else {<br />
    126. powerLevel.value -= timeDelay * Time.deltaTime;</p>
    127. <p> if(powerLevel.value == minLevel){<br />
    128. isMax = false;<br />
    129. }<br />
    130. }<br />
    131. }</p>
    132. <p> void CannonMovement(){</p>
    133. <p> if (Input.GetKey (KeyCode.UpArrow)) {<br />
    134. currentRotation.z += 50f * Time.deltaTime;<br />
    135. } else if (Input.GetKey (KeyCode.DownArrow)) {<br />
    136. currentRotation.z -= 50f * Time.deltaTime;<br />
    137. }</p>
    138. <p> currentRotation.z = Mathf.Clamp(currentRotation.z, minRot, maxRot);</p>
    139. <p> transform.rotation = Quaternion.Euler (currentRotation);<br />
    140. }</p>
    141. <p> void TouchCannonMovement(){<br />
    142. if(Input.touchCount > 0){<br />
    143. Touch touch = Input.GetTouch (0);</p>
    144. <p> if (touch.position.x < 300 && !isCharging && touch.position.y > 160) {</p>
    145. <p> if (touch.phase == TouchPhase.Moved) {<br />
    146. Vector2 touchDeltaPosition = touch.deltaPosition;<br />
    147. direction = touchDeltaPosition.normalized;</p>
    148. <p> if (direction.y > 0 && touchDeltaPosition.x > -10 && touchDeltaPosition.x < 10) {<br />
    149. currentRotation.z += 50f * Time.deltaTime;<br />
    150. } else if (direction.y < 0 && touchDeltaPosition.x > -10 && touchDeltaPosition.x < 10) {<br />
    151. currentRotation.z -= 50f * Time.deltaTime;<br />
    152. }</p>
    153. <p> }<br />
    154. }<br />
    155. }</p>
    156. <p> currentRotation.z = Mathf.Clamp(currentRotation.z, minRot, maxRot);</p>
    157. <p> transform.rotation = Quaternion.Euler (currentRotation);</p>
    158. <p> }<br />

  8. After creating the script, drag the script to the Cannon inspector as a component.
  9. Next we will create a power shot indicator for the cannon, this will tell how much force the shot will can reach the enemies. To do that go to GameObject and UI, select then the Slider.
  10. Set all the Components of the Slider as shown below to optimize the game.

    Slider

    tut60

    Background

    tut61

    Fill

    tut62

Creating Cannon Bullet

We will now create the object that will destroy enemies and structures when collide. To do that first locates the sprite inside the Sprites directory.

  1. Drag the sprite GameObject to the Scene View.
    tut57
  2. Next set the component of the Bomb GameObject.
    tut58
  3. Go to the Scripts folder and select Player. Create a C# script called Cannon Bullet.
  4. Write these block of code inside the CannonBullet Class:
    1. <br />
    2. public GameObject explosionEffect;<br />
    3. public bool isIdle;
    4. <p> private Rigidbody2D rigidBody2D;<br />
    5. private bool cannonShot, isExplode; </p>
    6. <p> void Awake(){<br />
    7. rigidBody2D = GetComponent<Rigidbody2D> ();<br />
    8. }</p>
    9. <p> // Use this for initialization<br />
    10. void Start () {<br />
    11. cannonShot = true;<br />
    12. }</p>
    13. <p> // Update is called once per frame<br />
    14. void Update () {<br />
    15. if (GameplayController.instance.gameInProgress) {<br />
    16. if (cannonShot && rigidBody2D.velocity.sqrMagnitude <= 0) {<br />
    17. DestroyBullet ();<br />
    18. }<br />
    19. }</p>
    20. <p> }</p>
    21. <p> void DestroyBullet(){<br />
    22. Destroy (gameObject);<br />
    23. if (!isExplode) {<br />
    24. isExplode = true;<br />
    25. GameObject newExplosionEffect = Instantiate (explosionEffect, transform.position, Quaternion.identity) as GameObject;<br />
    26. Destroy (newExplosionEffect, 3f);<br />
    27. }<br />
    28. }<br />

  5. After creating the script, drag the script to the Bomb inspector as a component.
  6. Lastly we will create an effect for the bomb. To do that go to GameObject and Select Particle System. Particle System is a built in module in Unity for creating a stunning effects for the Game.
  7. Rename the Particle System as Explosion Effect and set the components of the Particle System as shown below.
    tut59
  8. After creating the effect, Create a new Directory inside the Prefabs called FX, this directory will hold all the needed effects for the game. Then drag the effects inside the newly created directory to create a prefab and delete it because we will just spawn the gameobject everytime time the bomb explode.
  9. Next is attach all the needed Gameobject to the Script of the Bomb and Cannon. After that drag the Player and Bomb gameobject to Player directory to create a prefab, and then delete Bomb in the Scene View after that.

    Cannon

    tut63

    Bomb

    tut64

  10. Lastly create a Shot indicator that tells how much bomb is left to be shot. To do that go to the GameObject and UI, then select Image. And also a Text UI as a child for the Image and set there component as shown below.

    Image

    tut65

    Text

    tut66

Creating Camera Movement

We will create the camera that follow the bomb when shot. To that first create two GameObject, this will limit the Camera movement around the screen. Then set the component of the newly created gameobject.
tut67

After that go to Scripts directory then create a new directory called Camera. Inside the Camera directory create a C# script called CameraFollow.

Inside the CameraFollow script, create a certain variable that we will use. Write these variables inside the CameraFollow class.

  1. <br />
  2. public Transform target;<br />
  3. public Transform leftBound;<br />
  4. public Transform rightBound;<br />
  5. public float speed = 5f;<br />
  6. public bool isFollowing;</p>
  7. <p>private float arrowX;<br />
  8. public bool allowToMove;</p>
  9. <p>private float dragSpeed = 0.05f;<br />
  10. private float timeSinceShot;<br />
  11. private Vector3 velocity = Vector3.zero;<br />
  12. private Vector3 startPosition;<br />
  13. private Vector3 camPos;<br />

Note: When using a UI Components in the script you will just need to import some modules.

Then write this certain methods to make the camera work perfectly in the game.

  1. <br />
  2. // Use this for initialization<br />
  3. void Start () {<br />
  4. startPosition = transform.localPosition;<br />
  5. }</p>
  6. <p> // Update is called once per frame<br />
  7. void Update () {<br />
  8. if (GameplayController.instance.gameInProgress) {<br />
  9. if (isFollowing) {<br />
  10. if (GameObject.FindGameObjectWithTag ("Player Bullet") != null) {<br />
  11. MoveCameraFollow ();<br />
  12. }<br />
  13. } else {<br />
  14. if (!GameplayController.instance.player.GetChild (0).transform.GetComponent<Cannon> ().readyToShoot) {<br />
  15. MoveCameraBackToStart ();<br />
  16. AfterShotMoveAgain ();<br />
  17. allowToMove = false;<br />
  18. } else {<br />
  19. timeSinceShot = 0;<br />
  20. allowToMove = true;<br />
  21. }</p>
  22. <p> }</p>
  23. <p> if (Application.platform == RuntimePlatform.Android) {<br />
  24. TouchMoveCamera ();<br />
  25. } else if (Application.platform == RuntimePlatform.WindowsEditor) {<br />
  26. MoveCamera ();<br />
  27. }<br />
  28. }</p>
  29. <p> }</p>
  30. <p> void MoveCameraFollow(){<br />
  31. if(target != null){<br />
  32. transform.position = Vector3.Lerp (new Vector3 (transform.position.x, 0, -10f), new Vector3 (Mathf.Clamp (target.transform.position.x, leftBound.position.x, rightBound.position.x), 0, transform.position.z), Time.deltaTime * 10);<br />
  33. }<br />
  34. }</p>
  35. <p> void MoveCameraBackToStart(){<br />
  36. transform.position = Vector3.MoveTowards (transform.position, startPosition, Time.deltaTime * 5f);<br />
  37. }</p>
  38. <p> void AfterShotMoveAgain(){<br />
  39. timeSinceShot += Time.deltaTime;<br />
  40. if (timeSinceShot > 2f) {<br />
  41. if(startPosition == transform.position){<br />
  42. GameplayController.instance.player.GetChild (0).transform.GetComponent<Cannon> ().readyToShoot = true;<br />
  43. }<br />
  44. }<br />
  45. }</p>
  46. <p> void MoveCamera(){<br />
  47. arrowX = Input.GetAxis ("Horizontal");</p>
  48. <p> Transform cam = gameObject.transform;</p>
  49. <p> if (allowToMove) {<br />
  50. if (arrowX != 0) {<br />
  51. cam.position = cam.position + (new Vector3 (arrowX, 0, 0) * speed * Time.deltaTime);<br />
  52. float camX = cam.position.x;<br />
  53. camX = Mathf.Clamp (camX, leftBound.transform.position.x, rightBound.transform.position.x);<br />
  54. cam.position = new Vector3 (camX, cam.position.y, cam.position.z);<br />
  55. }<br />
  56. }<br />
  57. }</p>
  58. <p> void TouchMoveCamera(){<br />
  59. if(allowToMove){</p>
  60. <p> /*if(Input.touchCount > 0){<br />
  61. Touch touch = Input.GetTouch (0);</p>
  62. <p> Transform cam = gameObject.transform;</p>
  63. <p> if(touch.phase == TouchPhase.Began){<br />
  64. cam = touch.position;<br />
  65. }else if(touch.phase == TouchPhase.Moved){</p>
  66. <p> }<br />
  67. }*/</p>
  68. <p> if(Input.touchCount > 0){<br />
  69. Touch touch = Input.GetTouch (0);</p>
  70. <p> Transform cam = gameObject.transform;<br />
  71. if (touch.position.x > 300) {<br />
  72. if(touch.phase == TouchPhase.Began || touch.phase == TouchPhase.Stationary){<br />
  73. camPos = touch.position;<br />
  74. }else if(touch.phase == TouchPhase.Moved){<br />
  75. cam.position = cam.position + (new Vector3 ((camPos.x - touch.position.x), 0, 0) * dragSpeed * Time.deltaTime);<br />
  76. float camX = cam.position.x;<br />
  77. camX = Mathf.Clamp (camX, leftBound.transform.position.x, rightBound.transform.position.x);<br />
  78. cam.position = new Vector3 (camX, cam.position.y, cam.position.z);<br />
  79. }<br />
  80. }</p>
  81. <p> }</p>
  82. <p> }<br />
  83. }<br />

After that attach the script inside the Camera inspector and then attach all the needed gameobject to the script as shown below.
tut68

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment