GetKeyDown
How do I use Get Key Down
Below are practical examples compiled from projects for learning and reference purposes
Featured Snippets
File name: PhotonStatsGui.cs
Copy
53 public void Update()
54 {
55 if (Input.GetKeyDown(KeyCode.Tab) && Input.GetKey(KeyCode.LeftShift))
56 {
57 this.statsWindowOn = !this.statsWindowOn;
58 this.statsOn = true; // enable stats when showing the window
59 }
60 }
File name: QuitOnEscapeOrBack.cs
Copy
6 private void Update()
7 {
8 // "back" button of phone equals "Escape". quit app if that's pressed
9 if (Input.GetKeyDown(KeyCode.Escape))
10 {
11 Application.Quit();
12 }
13 }
File name: Game.cs
Copy
125 protected override void Update()
126 {
127 base.Update();
128
129 if (Input.GetKeyDown(KeyCode.Escape))
130 {
131 if (CurrentState == GameState.MainMenu)
132 {
133 Application.Quit();
134 }
135 else
136 {
137 Quit();
138 }
139 }
140 }
File name: GameManager.cs
Copy
172 void Update () {
173
174#if UNITY_EDITOR
175 if (Input.GetKeyDown(KeyCode.Z)) {
176 GameManager.Instance.GameState.Checkmate();
177 GameOver(p1,GameOverType.CHECKMATE); //TODO delete
178 } else if (Input.GetKeyDown(KeyCode.X)) {
179 GameManager.Instance.GameState.Checkmate();
180 GameOver(p2,GameOverType.CHECKMATE); //TODO delete
181 } else if (Input.GetKeyDown(KeyCode.C)) {
182 GameManager.Instance.GameState.Stalemate();
183 GameOver(p2,GameOverType.STALEMATE);
184 }
185#endif
186
187 if (!ready) return;
188 if (gameState.IsGameOver) return;
189
190
191 //EXPERIMENT_TIMER
192 if (whiteTurn) {
193 whiteTimer -= Time.deltaTime;
194 if (whiteTimer < 0 ) {
195 whiteTimer = 0;
196 GameManager.Instance.GameState.OutOfTime();
197 GameOver(p2, GameOverType.OUT_OF_TIME);
198 }
199 UpdateWhiteTimer();
200 } else {
201 blackTimer -= Time.deltaTime;
202 if (blackTimer < 0 ) {
203 blackTimer = 0;
204 GameManager.Instance.GameState.OutOfTime();
205 GameOver(p1, GameOverType.OUT_OF_TIME);
206 }
207 UpdateBlackTimer();
208 }
209 }
File name: PlayerMoveScript.cs
Copy
50 void Update () {
51
52 grounded = Physics2D.OverlapCircle(groundCheck.position, groundChecked, isGround);
53
54 if(transform.position.x > speedCount) {
55 speedCount += speedIncrease;
56 speedIncrease = speedIncrease * speedMultiplier;
57 moveSpeed = moveSpeed * speedMultiplier;
58 }
59
60 rigidbody.velocity = new Vector2 (moveSpeed, rigidbody.velocity.y);
61
62 if(Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0)) {
63
64 if(grounded) {
65 rigidbody.velocity = new Vector2 (rigidbody.velocity.x, jumpForce);
66 anim.SetTrigger ("Jump");
67 stopJumping = false;
68 }
69 }
70
71 if((Input.GetKey(KeyCode.Space) || Input.GetMouseButton(0)) && !stopJumping) {
72 if(jumpTimeCounter > 0) {
73 rigidbody.velocity = new Vector2 (rigidbody.velocity.x, jumpForce);
74 jumpTimeCounter -= Time.deltaTime;
75 }
76 }
77
78 if(Input.GetKeyUp (KeyCode.Space) || Input.GetMouseButtonUp(0)) {
79 jumpTimeCounter = 0;
80 stopJumping = true;
81 }
82
83 if(grounded) {
84 jumpTimeCounter = jumpTime;
85 }
86
87
88 scoreCount += pointsPerSeconds = Time.deltaTime;
89
90 if(scoreCount > highScoreCount) {
91 highScoreCount = scoreCount;
92
93 }
94 scoreText.text = "Score: " + Mathf.Round(scoreCount);
95
96 }
File name: GameplayController.cs
Copy
34 void Update () {
35 if(Input.GetKeyDown(KeyCode.Escape)){
36 PausePanel ();
37 }
38 }
File name: MainMenuController.cs
Copy
22 void Update(){
23 if(Input.GetKeyDown(KeyCode.Escape)){
24 if (!quitPanel.activeInHierarchy) {
25 quitPanel.SetActive (true);
26 } else {
27 quitPanel.SetActive (false);
28 }
29 }
30 }
File name: PlayerControl.cs
Copy
49 void ShootOrPauseControls()
50 {
51 if (!SystemScr.paused)
52 {
53 if (Input.GetMouseButtonDown(0) || Input.GetKeyDown(KeyCode.Space))
54 {
55 GameObject p_bullet = pools.GetPoolableObject("p_bullet");
56
57 if (p_bullet != null && maxBulletsOnScreen > 0)
58 {
59 sounds.PlaySoundsPlayer(0);
60 p_bullet.SetActive(true);
61 p_bullet.GetComponent
62 }
63 }
64 }
65 if (Input.GetKeyDown(KeyCode.Escape))
66 {
67 ui.Pause_btn();
68 }
69 }
File name: LevelController.cs
Copy
17 void Update () {
18 if(Input.GetKeyDown("Escape")){
19 SceneManager.LoadScene (SceneManager.GetActiveScene ().buildIndex - 1);
20 }
21 }
File name: MainMenuController.cs
Copy
26 void Update () {
27 if(Input.GetKeyDown(KeyCode.Escape)){
28 if (exitPanel.activeInHierarchy) {
29 exitPanel.SetActive (false);
30 } else {
31 exitPanel.SetActive (true);
32 }
33
34 if (settingsPanel.activeInHierarchy) {
35 settingsPanel.SetActive (false);
36 }
37
38 }
39 }
GetKeyDown 173 lượt xem
Gõ tìm kiếm nhanh...