Copying and Pasting cs Code

In cs, like in almost any computer programming language, reading data from a file can be tricky. You add extra lines of code to tell the computer what to do. Sometimes you can copy and paste these lines from other peoples’ code.

For example, you can follow the pattern in this listing:

  void TouchCannonShoot(){
   if(Input.touchCount > 0){
    Touch touch = Input.GetTouch (0);

    touchPos = Camera.main.ScreenToWorldPoint (touch.position);

    Vector2 touchRayHit = new Vector2 (touchPos.x, touchPos.y);

    RaycastHit2D hit = Physics2D.Raycast (touchRayHit , Vector2.zero);

    if(hit.collider != null){
     if(hit.collider.CompareTag("Player")){
      if(touch.phase == TouchPhase.Stationary){
       if (shot != 0) {
        UpdatePowerLevel ();
        isCharging = true;
       }
      }else if(touch.phase == TouchPhase.Ended){
       if (shot != 0) {
        GameObject newCannonBullet = Instantiate (cannonBullet, cannonTip.position, Quaternion.identity) as GameObject;
        newCannonBullet.transform.GetComponent ().AddForce (cannonTip.right * powerLevel.value, ForceMode2D.Impulse);
        if (GameController.instance != null && MusicController.instance != null) {
         if (GameController.instance.isMusicOn) {
          audioSource.PlayOneShot (cannonShot);
         }
        }
        shot--;
        powerLevel.value = 0;
        readyToShoot = false;
        isCharging = false;
       }
      }
     }
    }

    /*if(touch.phase == TouchPhase.Stationary){
     if (shot != 0) {
      UpdatePowerLevel ();
     }
    }else if(touch.phase == TouchPhase.Ended){
     if (shot != 0) {
      GameObject newCannonBullet = Instantiate (cannonBullet, cannonTip.position, Quaternion.identity) as GameObject;
      newCannonBullet.transform.GetComponent ().AddForce (cannonTip.right * powerLevel.value, ForceMode2D.Impulse);
      if (GameController.instance != null && MusicController.instance != null) {
       if (GameController.instance.isMusicOn) {
        audioSource.PlayOneShot (cannonShot);
       }
      }
      shot--;
      powerLevel.value = 0;
      readyToShoot = false;
     }
    }*/

   }
  }