GetKey









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

Featured Snippets


File name: JumpAndRunMovement.cs Copy
52     void UpdateJumping()
53     {
54         if( Input.GetKey( KeyCode.Space ) == true && m_IsGrounded == true )
55         {
56             m_Animator.SetTrigger( "IsJumping" );
57             m_Body.AddForce( Vector2.up * JumpForce );
58             m_PhotonView.RPC( "DoJump", PhotonTargets.Others );
59         }
60     }
File name: OnClickRequestOwnership.cs Copy
8     public void OnClick()
9     {
10         if( Input.GetKey( KeyCode.LeftShift ) || Input.GetKey( KeyCode.RightShift ) )
11         {
12             Vector3 colVector = new Vector3( Random.Range( 0.0f, 1.0f ), Random.Range( 0.0f, 1.0f ), Random.Range( 0.0f, 1.0f ) );
13             this.photonView.RPC( "ColorRpc", PhotonTargets.AllBufferedViaServer, colVector );
14         }
15         else
16         {
17             if( this.photonView.ownerId == PhotonNetwork.player.ID )
18             {
19                 Debug.Log( "Not requesting ownership. Already mine." );
20                 return;
21             }
22
23             this.photonView.RequestOwnership();
24         }
25     }
File name: PlayerControls.cs Copy
19  void Update () {
20   var vel = rb2d.velocity;
21   if (Input.GetKey (moveUp)) {
22    vel.y = speed;
23   } else if (Input.GetKey (moveDown)) {
24    vel.y = -speed;
25   } else if (!Input.anyKey) {
26    vel.y = 0;
27   }
28   rb2d.velocity = vel;
29
30   var pos = transform.position;
31   if (pos.y > boundY) {
32    pos.y = boundY;
33   } else if (pos.y < -boundY) {
34    pos.y = -boundY;
35   }
36   transform.position = pos;
37  }
File name: RPGMovement.cs Copy
106     void UpdateForwardMovement()
107     {
108         if( Input.GetKey( KeyCode.W ) == true )
109         {
110             m_CurrentMovement = transform.forward * ForwardSpeed;
111         }
112     }
File name: RPGMovement.cs Copy
114     void UpdateBackwardMovement()
115     {
116         if( Input.GetKey( KeyCode.S ) == true )
117         {
118             m_CurrentMovement = -transform.forward * BackwardSpeed;
119         }
120     }
File name: RPGMovement.cs Copy
122     void UpdateStrafeMovement()
123     {
124         if( Input.GetKey( KeyCode.Q ) == true )
125         {
126             m_CurrentMovement = -transform.right * StrafeSpeed;
127         }
128
129         if( Input.GetKey( KeyCode.E ) == true )
130         {
131             m_CurrentMovement = transform.right * StrafeSpeed;
132         }
133     }
File name: RPGMovement.cs Copy
135     void UpdateRotateMovement()
136     {
137         if( Input.GetKey( KeyCode.A ) == true )
138         {
139             m_CurrentTurnSpeed = -RotateSpeed;
140             transform.Rotate( 0, -RotateSpeed * Time.deltaTime, 0 );
141         }
142
143         if( Input.GetKey( KeyCode.D ) == true )
144         {
145             m_CurrentTurnSpeed = RotateSpeed;
146             transform.Rotate( 0, RotateSpeed * Time.deltaTime, 0 );
147         }
148     }
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: MoveByKeys.cs Copy
37     public void FixedUpdate()
38     {
39         if (!photonView.isMine)
40         {
41             return;
42         }
43
44         if (Input.GetKey(KeyCode.A))
45         {
46             transform.position += Vector3.left*(this.Speed*Time.deltaTime);
47         }
48
49         if (Input.GetKey(KeyCode.D))
50         {
51             transform.position += Vector3.right*(this.Speed*Time.deltaTime);
52         }
53
54         // jumping has a simple "cooldown" time but you could also jump in the air
55         if (this.jumpingTime <= 0.0f)
56         {
57             if (this.body != null || this.body2d != null)
58             {
59                 // obj has a Rigidbody and can jump (AddForce)
60                 if (Input.GetKey(KeyCode.Space))
61                 {
62                     this.jumpingTime = this.JumpTimeout;
63
64                     Vector2 jump = Vector2.up*this.JumpForce;
65                     if (this.body2d != null)
66                     {
67                         this.body2d.AddForce(jump);
68                     }
69                     else if (this.body != null)
70                     {
71                         this.body.AddForce(jump);
72                     }
73                 }
74             }
75         }
76         else
77         {
78             this.jumpingTime -= Time.deltaTime;
79         }
80
81         // 2d objects can't be moved in 3d "forward"
82         if (!this.isSprite)
83         {
84             if (Input.GetKey(KeyCode.W))
85             {
86                 transform.position += Vector3.forward*(this.Speed*Time.deltaTime);
87             }
88
89             if (Input.GetKey(KeyCode.S))
90             {
91                 transform.position -= Vector3.forward*(this.Speed*Time.deltaTime);
92             }
93         }
94     }
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     }

GetKey 154 lượt xem

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