GetButtonDown
How do I use Get Button Down
Below are practical examples compiled from projects for learning and reference purposes
Featured Snippets
File name: PickupController.cs
Copy
151 void Update()
152 {
153 if (isControllable)
154 {
155 if (Input.GetButtonDown("Jump"))
156 {
157 lastJumpButtonTime = Time.time;
158 }
159
160 UpdateSmoothedMovementDirection();
161
162 // Apply gravity
163 // - extra power jump modifies gravity
164 // - controlledDescent mode modifies gravity
165 ApplyGravity();
166
167 // Apply jumping logic
168 ApplyJumping();
169
170
171 // Calculate actual motion
172 Vector3 movement = moveDirection * moveSpeed + new Vector3(0, verticalSpeed, 0) + inAirVelocity;
173 movement *= Time.deltaTime;
174
175 //Debug.Log(movement.x.ToString("0.000") + ":" + movement.z.ToString("0.000"));
176
177 // Move the controller
178 CharacterController controller = GetComponent
179 collisionFlags = controller.Move(movement);
180
181 }
182
183 // PUN: if a remote position is known, we smooth-move to it (being late(r) but smoother)
184 if (this.remotePosition != Vector3.zero)
185 {
186 transform.position = Vector3.Lerp(transform.position, this.remotePosition, Time.deltaTime * this.RemoteSmoothing);
187 }
188
189 velocity = (transform.position - lastPos)*25;
190
191 // ANIMATION sector
192 if (_animation)
193 {
194 if (_characterState == PickupCharacterState.Jumping)
195 {
196 if (!jumpingReachedApex)
197 {
198 _animation[jumpPoseAnimation.name].speed = jumpAnimationSpeed;
199 _animation[jumpPoseAnimation.name].wrapMode = WrapMode.ClampForever;
200 _animation.CrossFade(jumpPoseAnimation.name);
201 }
202 else
203 {
204 _animation[jumpPoseAnimation.name].speed = -landAnimationSpeed;
205 _animation[jumpPoseAnimation.name].wrapMode = WrapMode.ClampForever;
206 _animation.CrossFade(jumpPoseAnimation.name);
207 }
208 }
209 else
210 {
211 if (_characterState == PickupCharacterState.Idle)
212 {
213 _animation.CrossFade(idleAnimation.name);
214 }
215 else if (_characterState == PickupCharacterState.Running)
216 {
217 _animation[runAnimation.name].speed = runMaxAnimationSpeed;
218 if (this.isControllable)
219 {
220 _animation[runAnimation.name].speed = Mathf.Clamp(velocity.magnitude, 0.0f, runMaxAnimationSpeed);
221 }
222 _animation.CrossFade(runAnimation.name);
223 }
224 else if (_characterState == PickupCharacterState.Trotting)
225 {
226 _animation[walkAnimation.name].speed = trotMaxAnimationSpeed;
227 if (this.isControllable)
228 {
229 _animation[walkAnimation.name].speed = Mathf.Clamp(velocity.magnitude, 0.0f, trotMaxAnimationSpeed);
230 }
231 _animation.CrossFade(walkAnimation.name);
232 }
233 else if (_characterState == PickupCharacterState.Walking)
234 {
235 _animation[walkAnimation.name].speed = walkMaxAnimationSpeed;
236 if (this.isControllable)
237 {
238 _animation[walkAnimation.name].speed = Mathf.Clamp(velocity.magnitude, 0.0f, walkMaxAnimationSpeed);
239 }
240 _animation.CrossFade(walkAnimation.name);
241 }
242
243 if (_characterState != PickupCharacterState.Running)
244 {
245 _animation[runAnimation.name].time = 0.0f;
246 }
247 }
248 }
249 // ANIMATION sector
250
251 // Set rotation to the move direction
252 if (IsGrounded())
253 {
254 // a specialty of this controller: you can disable rotation!
255 if (DoRotate)
256 {
257 transform.rotation = Quaternion.LookRotation(moveDirection);
258 }
259 }
260 else
261 {
262 /* This causes choppy behaviour when colliding with SIDES
263 * Vector3 xzMove = velocity;
264 xzMove.y = 0;
265 if (xzMove.sqrMagnitude > 0.001f)
266 {
267 transform.rotation = Quaternion.LookRotation(xzMove);
268 }*/
269 }
270
271 // We are in jump mode but just became grounded
272 if (IsGrounded())
273 {
274 lastGroundedTime = Time.time;
275 inAirVelocity = Vector3.zero;
276 if (jumping)
277 {
278 jumping = false;
279 SendMessage("DidLand", SendMessageOptions.DontRequireReceiver);
280 }
281 }
282
283 lastPos = transform.position;
284 }
File name: IdleRunJump.cs
Copy
33 void Update ()
34 {
35 if( m_PhotonView.isMine == false && PhotonNetwork.connected == true )
36 {
37 return;
38 }
39
40 if (animator)
41 {
42 AnimatorStateInfo stateInfo = animator.GetCurrentAnimatorStateInfo(0);
43
44 if (stateInfo.IsName("Base Layer.Run"))
45 {
46 if (Input.GetButton("Fire1")) animator.SetBool("Jump", true);
47 }
48 else
49 {
50 animator.SetBool("Jump", false);
51 }
52
53 if(Input.GetButtonDown("Fire2") && animator.layerCount >= 2)
54 {
55 animator.SetBool("Hi", !animator.GetBool("Hi"));
56 }
57
58
59 float h = Input.GetAxis("Horizontal");
60 float v = Input.GetAxis("Vertical");
61
62 if( v < 0 )
63 {
64 v = 0;
65 }
66
67 animator.SetFloat( "Speed", h*h+v*v );
68 animator.SetFloat( "Direction", h, DirectionDampTime, Time.deltaTime );
69
70 float direction = animator.GetFloat( "Direction" );
71
72 float targetSpeedModifier = Mathf.Abs( v );
73
74 if( Mathf.Abs( direction ) > 0.2f )
75 {
76 targetSpeedModifier = TurnSpeedModifier;
77 }
78
79 m_SpeedModifier = Mathf.MoveTowards( m_SpeedModifier, targetSpeedModifier, Time.deltaTime * 25f );
80
81 Vector3 speed = transform.forward * SynchronizedMaxSpeed * m_SpeedModifier;
82 float turnSpeed = direction * SynchronizedTurnSpeed;
83
84 /*float moveDistance = Vector3.Distance( transform.position, m_LastPosition ) / Time.deltaTime;
85
86 if( moveDistance < 4f && turnSpeed == 0f )
87 {
88 speed = transform.forward * moveDistance;
89 }*/
90
91 //Debug.Log( moveDistance );
92 //Debug.Log( speed + " - " + speed.magnitude + " - " + speedModifier + " - " + h + " - " + v );
93
94 m_TransformView.SetSynchronizedValues( speed, turnSpeed );
95
96 //m_LastPosition = transform.position;
97 }
98 }
File name: PlayerCtrl.cs
Copy
37 void Update () {
38 isGrounded = Physics2D.OverlapBox(new Vector2(feet.position.x, feet.position.y), new Vector2(boxWidth, boxHeight), 360.0f, whatIsGround);
39
40 float playerSpeed = Input.GetAxisRaw("Horizontal"); // value will be 1, -1 or 0
41
42 playerSpeed *= speedBoost;
43
44 if (playerSpeed != 0)
45 MovePlayer(playerSpeed);
46 else
47 StopMoving();
48
49 if (Input.GetButtonDown("Jump"))
50 Jump();
51
52 if (Input.GetButtonDown("Fire1"))
53 {
54 FireBullets();
55 }
56
57 ShowFalling();
58
59 if (leftPressed)
60 MovePlayer(-speedBoost);
61
62 if (rightPressed)
63 MovePlayer(speedBoost);
64 }
File name: ScrollHelp.cs
Copy
65 public void Update()
66 {
67 if (Input.GetButtonDown("Fire1"))
68 {
69 touchX = Input.mousePosition.x;
70 isUpdate = false;
71 updateDots();
72 }
73 else if (Input.GetButton("Fire1"))//su kien giu chuot
74 {
75 deltaX = Input.mousePosition.x - touchX;
76
77 rect1.x += deltaX;
78 rect2.x += deltaX;
79 rect3.x += deltaX;
80 rect4.x += deltaX;
81
82 touchX = Input.mousePosition.x;
83
84 }
85 else if (Input.GetButtonUp("Fire1"))
86 {
87 Current = currentBg(rect1.x);
88 offsetX = -538 * Current - 10 * Current - rect1.x;
89 step = offsetX * 5;
90 isUpdate = true;
91 rxs[0] = rect1.x + offsetX;
92 rxs[1] = rect2.x + offsetX;
93 rxs[2] = rect3.x + offsetX;
94 rxs[3] = rect4.x + offsetX;
95 }
96
97
98 if (isUpdate)
99 {
100 if (offsetX < 0)
101 {
102 if (rect1.x > rxs[0])
103 {
104 rect1.x += step * Time.deltaTime;
105 rect2.x += step * Time.deltaTime;
106 rect3.x += step * Time.deltaTime;
107 rect4.x += step * Time.deltaTime;
108 }
109 else
110 {
111 rect1.x = rxs[0];
112 rect2.x = rxs[1];
113 rect3.x = rxs[2];
114 rect4.x = rxs[3];
115 isUpdate = false;
116 updateDots();
117 }
118 }
119 else
120 {
121 if (rect1.x < rxs[0])
122 {
123 rect1.x += step * Time.deltaTime;
124 rect2.x += step * Time.deltaTime;
125 rect3.x += step * Time.deltaTime;
126 rect4.x += step * Time.deltaTime;
127 }
128 else
129 {
130 rect1.x = rxs[0];
131 rect2.x = rxs[1];
132 rect3.x = rxs[2];
133 rect4.x = rxs[3];
134 isUpdate = false;
135 updateDots();
136 }
137 }
138 }
139 }
File name: TouchText.cs
Copy
33 public void LateUpdate()
34 {
35 if (InputController.Enabled && InputController.Name == InputNames.MAINMENU)
36 {
37 if (Input.GetButtonDown("Fire1") && InputController.IsScreen)
38 {
39 SceneManager.LoadScene("MapScreen");
40 }
41 else if (Input.GetMouseButtonUp(0))
42 {
43 InputController.IsScreen = true;
44 }
45 }
46 }
File name: Maps.cs
Copy
51 void Update()
52 {
53 if (Input.GetButtonDown("Fire1"))
54 {
55 touchX = Input.mousePosition.x;
56 isUpdate = false;
57 }
58 else if (Input.GetButton("Fire1"))
59 {
60 deltaX = Input.mousePosition.x - touchX;
61 transform.localPosition += new Vector3(deltaX / 100, transform.localPosition.y, transform.localPosition.z);
62 touchX = Input.mousePosition.x;
63
64 current = currentWorldMap(transform.localPosition.x);
65
66 offsetX = -8 * current - transform.localPosition.x;
67 if (Mathf.Abs(offsetX) >= 0.3f)
68 {
69 isDragged = true;
70 }
71 UpdateDots();
72 }
73 else if (Input.GetButtonUp("Fire1"))
74 {
75 current = currentWorldMap(transform.localPosition.x);
76
77 offsetX = -8 * current - transform.localPosition.x;
78 rx = transform.position.x + offsetX;
79
80 isUpdate = true;
81 }
82
83 if (isUpdate)
84 {
85 updateMaps();
86 }
87 }
File name: InputProcessor.cs
Copy
27 public void Update()
28 {
29 if (!InputController.Enabled) return;
30 calculateTransform();
31 if (Input.GetButtonDown("Fire1"))
32 {
33 if (checkBounds(rect, convertTo800x480(Input.mousePosition)))
34 {
35 isTouchedDown = true;
36 if(gameObject.GetComponent
37 gameObject.GetComponent
38 InputController.IsScreen = false;
39 }
40 }
41 else if (Input.GetButtonUp("Fire1"))
42 {
43 if (gameObject.GetComponent
44 gameObject.GetComponent
45 Vector2 mouse = convertTo800x480(Input.mousePosition);
46 if (isTouchedDown && checkBounds(rect, convertTo800x480(Input.mousePosition)))
47 {
48 if(gameObject.GetComponent
49 gameObject.GetComponent
50 }
51 isTouchedDown = false;
52 }
53 }
GetButtonDown 184 lượt xem
Gõ tìm kiếm nhanh...