Cannon









How do I use Cannon
Below are practical examples compiled from projects for learning and reference purposes

Featured Snippets


File name: CameraFollow.cs Copy
30  void Update () {
31   if (GameplayController.instance.gameInProgress) {
32    if (isFollowing) {
33     if (GameObject.FindGameObjectWithTag ("Player Bullet") != null) {
34      MoveCameraFollow ();
35     }
36    } else {
37     if (!GameplayController.instance.player.GetChild (0).transform.GetComponent ().readyToShoot) {
38      MoveCameraBackToStart ();
39      AfterShotMoveAgain ();
40      allowToMove = false;
41     } else {
42      timeSinceShot = 0;
43      allowToMove = true;
44     }
45
46    }
47
48    if (Application.platform == RuntimePlatform.Android) {
49     TouchMoveCamera ();
50    } else if (Application.platform == RuntimePlatform.WindowsEditor) {
51     MoveCamera ();
52    }
53   }
54
55  }
File name: CameraFollow.cs Copy
67  void AfterShotMoveAgain(){
68   timeSinceShot += Time.deltaTime;
69   if (timeSinceShot > 2f) {
70    if(startPosition == transform.position){
71     GameplayController.instance.player.GetChild (0).transform.GetComponent ().readyToShoot = true;
72    }
73   }
74  }
File name: GameplayController.cs Copy
46  void Update () {
47   if (gameInProgress) {
48    GameIsOnPlay ();
49    DistanceBetweenCannonAndBullet ();
50   }
51
52
53   if(GameController.instance != null){
54    UpdateGameplayController ();
55   }
56
57  }
File name: GameplayController.cs Copy
175  public int PlayerBullet(){
176   int playerBullet = GameObject.FindGameObjectWithTag ("Player").transform.GetChild (0).transform.GetComponent ().shot;
177   return playerBullet;
178  }
File name: GameplayController.cs Copy
198  void DistanceBetweenCannonAndBullet(){
199   GameObject[] bullet = GameObject.FindGameObjectsWithTag ("Player Bullet");
200   foreach (GameObject distanceToBullet in bullet) {
201    if (!distanceToBullet.transform.GetComponent ().isIdle) {
202     if (distanceToBullet.transform.position.x - player.position.x > distance) {
203      camera.isFollowing = true;
204      checkGameStatus = true;
205      timeSinceStartedShot = Time.time;
206      TimeSinceShot ();
207      camera.target = distanceToBullet.transform;
208     } else {
209      if(PlayerBullet() == 0){
210       camera.isFollowing = true;
211       checkGameStatus = true;
212       timeSinceStartedShot = Time.time;
213       TimeSinceShot ();
214       camera.target = distanceToBullet.transform;
215      }
216     }
217    }
218   }
219   /*if (GameObject.FindGameObjectWithTag ("Player Bullet") != null) {
220    if (!GameObject.FindGameObjectWithTag ("Player Bullet").transform.GetComponent ().isIdle) {
221     Transform distanceToBullet = GameObject.FindGameObjectWithTag ("Player Bullet").transform;
222     if (distanceToBullet.position.x - player.position.x > distance) {
223      camera.isFollowing = true;
224      checkGameStatus = true;
225      TimeSinceShot ();
226      camera.target = distanceToBullet;
227     }
228    }
229
230   }*/
231  }
File name: GameplayController.cs Copy
233  void TimeSinceShot(){
234   time += Time.deltaTime;
235   if (time > 3f) {
236    time = 0f;
237    GameObject.FindGameObjectWithTag ("Player Bullet").transform.GetComponent ().isIdle = true;
238   }
239
240  }
File name: Cannon.cs Copy
43  void Update () {
44   if (GameplayController.instance.gameInProgress) {
45    if (readyToShoot) {
46     if(Application.platform == RuntimePlatform.Android){
47      TouchCannonShoot ();
48     }else if(Application.platform == RuntimePlatform.WindowsEditor){
49      CannonShoot ();
50     }
51
52    }
53
54    if(Application.platform == RuntimePlatform.Android){
55     TouchCannonMovement ();
56    }else if(Application.platform == RuntimePlatform.WindowsEditor){
57     CannonMovement ();
58    }
59
60   }
61  }
File name: Cannon.cs Copy
63  void TouchCannonShoot(){
64   if(Input.touchCount > 0){
65    Touch touch = Input.GetTouch (0);
66
67    touchPos = Camera.main.ScreenToWorldPoint (touch.position);
68
69    Vector2 touchRayHit = new Vector2 (touchPos.x, touchPos.y);
70
71    RaycastHit2D hit = Physics2D.Raycast (touchRayHit , Vector2.zero);
72
73    if(hit.collider != null){
74     if(hit.collider.CompareTag("Player")){
75      if(touch.phase == TouchPhase.Stationary){
76       if (shot != 0) {
77        UpdatePowerLevel ();
78        isCharging = true;
79       }
80      }else if(touch.phase == TouchPhase.Ended){
81       if (shot != 0) {
82        GameObject newCannonBullet = Instantiate (cannonBullet, cannonTip.position, Quaternion.identity) as GameObject;
83        newCannonBullet.transform.GetComponent ().AddForce (cannonTip.right * powerLevel.value, ForceMode2D.Impulse);
84        if (GameController.instance != null && MusicController.instance != null) {
85         if (GameController.instance.isMusicOn) {
86          audioSource.PlayOneShot (cannonShot);
87         }
88        }
89        shot--;
90        powerLevel.value = 0;
91        readyToShoot = false;
92        isCharging = false;
93       }
94      }
95     }
96    }
97
98    /*if(touch.phase == TouchPhase.Stationary){
99     if (shot != 0) {
100      UpdatePowerLevel ();
101     }
102    }else if(touch.phase == TouchPhase.Ended){
103     if (shot != 0) {
104      GameObject newCannonBullet = Instantiate (cannonBullet, cannonTip.position, Quaternion.identity) as GameObject;
105      newCannonBullet.transform.GetComponent ().AddForce (cannonTip.right * powerLevel.value, ForceMode2D.Impulse);
106      if (GameController.instance != null && MusicController.instance != null) {
107       if (GameController.instance.isMusicOn) {
108        audioSource.PlayOneShot (cannonShot);
109       }
110      }
111      shot--;
112      powerLevel.value = 0;
113      readyToShoot = false;
114     }
115    }*/
116
117   }
118  }
File name: Cannon.cs Copy
120  void CannonShoot(){
121   if (Input.GetKey (KeyCode.Space)) {
122    if(shot != 0){
123     UpdatePowerLevel ();
124    }
125   }else if(Input.GetKeyUp(KeyCode.Space)){
126    if (shot != 0) {
127     GameObject newCannonBullet = Instantiate (cannonBullet, cannonTip.position, Quaternion.identity) as GameObject;
128     newCannonBullet.transform.GetComponent ().AddForce (cannonTip.right * powerLevel.value, ForceMode2D.Impulse);
129     if (GameController.instance != null && MusicController.instance != null) {
130      if (GameController.instance.isMusicOn) {
131       audioSource.PlayOneShot (cannonShot);
132      }
133     }
134     shot--;
135     powerLevel.value = 0;
136     readyToShoot = false;
137    }
138   }
139
140  }
File name: Cannon.cs Copy
159  void CannonMovement(){
160
161   if (Input.GetKey (KeyCode.UpArrow)) {
162    currentRotation.z += 50f * Time.deltaTime;
163   } else if (Input.GetKey (KeyCode.DownArrow)) {
164    currentRotation.z -= 50f * Time.deltaTime;
165   }
166
167   currentRotation.z = Mathf.Clamp(currentRotation.z, minRot, maxRot);
168
169   transform.rotation = Quaternion.Euler (currentRotation);
170  }

Download file with original file name:Cannon

Cannon 144 lượt xem

Gõ tìm kiếm nhanh...