Timer
How do I use Timer
Below are practical examples compiled from projects for learning and reference purposes
Featured Snippets
File name: frmLuaChon.cs
Copy
36 private void timer1_Tick(object sender, EventArgs e)
37 {
38 lbluachon.Text = lbluachon.Text.Substring(lbluachon.Text.Length - 1, 1) + lbluachon.Text.Substring(0, lbluachon.Text.Length - 1);
39 }
File name: frmThi.cs
Copy
225 private void timer1_Tick(object sender, EventArgs e)
226 {
227 p--;
228 lbtime.Text =p.ToString() +" phut";
229 if (p==0)
230 {
231 timer1.Stop();
232 MessageBox.Show("Da het gio lam bai");
233 }
234 }
File name: frmThiThat.cs
Copy
190 private void frmThiThat_Load(object sender, EventArgs e)
191 {
192
193
194 lbNgaythang.Text = DateTime.Now.Day.ToString() + "/" + DateTime.Now.Month.ToString() + "/" + DateTime.Now.Year.ToString();
195
196 {
197 try
198 {
199 if (DeThi == true)
200 Load_DeThiThat();
201 else
202 Load_DeThiThat();
203 TaoBangRandomCauTraLoi();
204 LoadCauHoiVaoControl();
205 timer1.Start();
206
207 btPre.Enabled = false;
208 }
209 catch (Exception ex)
210 { MessageBox.Show(ex.Message); }
211 }
212
213 }
File name: frmThiThat.cs
Copy
261 private void btketthuc_Click(object sender, EventArgs e)
262 {
263 if (MessageBox.Show("Bạn có chắc chắn kết thúc không ?" , "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
264 {
265 this.Hide();
266 try
267 {
268 timer1.Stop();
269 btketthuc.Enabled = false;
270
271 GhiLaiDapAnTS();
272 SoCauDung = 0;
273 for (int i = 0; i < SoCauHoi; i++)
274 {
275 if (BangDeThi.Rows[i][6].ToString().ToUpper() == BangDeThi.Rows[i][7].ToString().ToUpper())
276 SoCauDung++;
277
278 }
279
280 diem = SoCauDung * 2;
281 MessageBox.Show("Đúng " + SoCauDung.ToString() + " câu " + " Bạn được " + diem.ToString() + " điểm ");
282 this.Close();
283 string ten = lbhoten.Text;
284 string mats = lbmathisinh.Text;
285 string diem1 = diem.ToString();
286 string socaudung = SoCauDung.ToString();
287 frmKetQua ketqua = new frmKetQua(ten,mats,diem1,socaudung);
288 ketqua.ShowDialog();
289 //them form ket qua tai day
290
291 }
292 catch (Exception ex)
293 { MessageBox.Show(ex.Message); }
294 }
295 else
296 { return; }
297 }
File name: frmThiThat.cs
Copy
301 private void timer1_Tick_1(object sender, EventArgs e)
302 {
303 timer1.Interval = 1000;
304
305 s--;
306 th--;
307 if (th == 0)
308 {
309 timer1.Stop();
310 MessageBox.Show("Hết giờ làm bài!", "Thong bao");
311 checkBox1.Checked = false;
312 checkBox2.Checked = false;
313 checkBox3.Checked = false;
314 checkBox4.Checked = false;
315
316 GhiLaiDapAnTS();
317 SoCauDung = 0;
318 for (int i = 0; i < SoCauHoi; i++)
319 {
320 if (BangDeThi.Rows[i][6].ToString().ToUpper() == BangDeThi.Rows[i][7].ToString().ToUpper())
321 SoCauDung++;
322 }
323 int diem;
324 diem = SoCauDung * 2;
325 MessageBox.Show("Đúng " + SoCauDung.ToString()+" câu " + " Bạn được " + diem.ToString() + " điểm ");
326 this.Close();
327
328 }
329 if (s == 0)
330 {
331 p--;
332 if (p == 0)
333 {
334 s = 60;
335 th = s;
336 }
337 s = 60;
338 }
339
340 lblHour.Text = h.ToString() + " : " + p.ToString() + " : " + s.ToString();
341
342 }
File name: PickupCamera.cs
Copy
94 void Apply( Transform dummyTarget, Vector3 dummyCenter )
95 {
96 // Early out if we don't have a target
97 if( !controller )
98 return;
99
100 Vector3 targetCenter = _target.position + centerOffset;
101 Vector3 targetHead = _target.position + headOffset;
102
103 // DebugDrawStuff();
104
105 // Calculate the current & target rotation angles
106 float originalTargetAngle = _target.eulerAngles.y;
107 float currentAngle = cameraTransform.eulerAngles.y;
108
109 // Adjust real target angle when camera is locked
110 float targetAngle = originalTargetAngle;
111
112 // When pressing Fire2 (alt) the camera will snap to the target direction real quick.
113 // It will stop snapping when it reaches the target
114 if( Input.GetButton( "Fire2" ) )
115 snap = true;
116
117 if( snap )
118 {
119 // We are close to the target, so we can stop snapping now!
120 if( AngleDistance( currentAngle, originalTargetAngle ) < 3.0f )
121 snap = false;
122
123 currentAngle = Mathf.SmoothDampAngle( currentAngle, targetAngle, ref angleVelocity, snapSmoothLag, snapMaxSpeed );
124 }
125 // Normal camera motion
126 else
127 {
128 if( controller.GetLockCameraTimer() < lockCameraTimeout )
129 {
130 targetAngle = currentAngle;
131 }
132
133 // Lock the camera when moving backwards!
134 // * It is really confusing to do 180 degree spins when turning around.
135 if( AngleDistance( currentAngle, targetAngle ) > 160 && controller.IsMovingBackwards() )
136 targetAngle += 180;
137
138 currentAngle = Mathf.SmoothDampAngle( currentAngle, targetAngle, ref angleVelocity, angularSmoothLag, angularMaxSpeed );
139 }
140
141
142 // When jumping don't move camera upwards but only down!
143 if( controller.IsJumping() )
144 {
145 // We'd be moving the camera upwards, do that only if it's really high
146 float newTargetHeight = targetCenter.y + height;
147 if( newTargetHeight < targetHeight || newTargetHeight - targetHeight > 5 )
148 targetHeight = targetCenter.y + height;
149 }
150 // When walking always update the target height
151 else
152 {
153 targetHeight = targetCenter.y + height;
154 }
155
156 // Damp the height
157 float currentHeight = cameraTransform.position.y;
158 currentHeight = Mathf.SmoothDamp( currentHeight, targetHeight, ref heightVelocity, heightSmoothLag );
159
160 // Convert the angle into a rotation, by which we then reposition the camera
161 Quaternion currentRotation = Quaternion.Euler( 0, currentAngle, 0 );
162
163 // Set the position of the camera on the x-z plane to:
164 // distance meters behind the target
165 cameraTransform.position = targetCenter;
166 cameraTransform.position += currentRotation * Vector3.back * distance;
167
168 // Set the height of the camera
169 cameraTransform.position = new Vector3( cameraTransform.position.x, currentHeight, cameraTransform.position.z );
170
171 // Always look at the target
172 SetUpRotation( targetCenter, targetHead );
173 }
File name: PickupController.cs
Copy
308 void UpdateSmoothedMovementDirection()
309 {
310 Transform cameraTransform = Camera.main.transform;
311 bool grounded = IsGrounded();
312
313 // Forward vector relative to the camera along the x-z plane
314 Vector3 forward = cameraTransform.TransformDirection(Vector3.forward);
315 forward.y = 0;
316 forward = forward.normalized;
317
318 // Right vector relative to the camera
319 // Always orthogonal to the forward vector
320 Vector3 right = new Vector3(forward.z, 0, -forward.x);
321
322 float v = Input.GetAxisRaw("Vertical");
323 float h = Input.GetAxisRaw("Horizontal");
324
325 // Are we moving backwards or looking backwards
326 if (v < -0.2f)
327 movingBack = true;
328 else
329 movingBack = false;
330
331 bool wasMoving = isMoving;
332 isMoving = Mathf.Abs(h) > 0.1f || Mathf.Abs(v) > 0.1f;
333
334 // Target direction relative to the camera
335 Vector3 targetDirection = h * right + v * forward;
336 // Debug.Log("targetDirection " + targetDirection);
337
338 // Grounded controls
339 if (grounded)
340 {
341 // Lock camera for short period when transitioning moving & standing still
342 lockCameraTimer += Time.deltaTime;
343 if (isMoving != wasMoving)
344 lockCameraTimer = 0.0f;
345
346 // We store speed and direction seperately,
347 // so that when the character stands still we still have a valid forward direction
348 // moveDirection is always normalized, and we only update it if there is user input.
349 if (targetDirection != Vector3.zero)
350 {
351 // If we are really slow, just snap to the target direction
352 if (moveSpeed < walkSpeed * 0.9f && grounded)
353 {
354 moveDirection = targetDirection.normalized;
355 }
356 // Otherwise smoothly turn towards it
357 else
358 {
359 moveDirection = Vector3.RotateTowards(moveDirection, targetDirection, rotateSpeed * Mathf.Deg2Rad * Time.deltaTime, 1000);
360
361 moveDirection = moveDirection.normalized;
362 }
363 }
364
365 // Smooth the speed based on the current target direction
366 float curSmooth = speedSmoothing * Time.deltaTime;
367
368 // Choose target speed
369 //* We want to support analog input but make sure you cant walk faster diagonally than just forward or sideways
370 float targetSpeed = Mathf.Min(targetDirection.magnitude, 1.0f);
371
372 _characterState = PickupCharacterState.Idle;
373
374 // Pick speed modifier
375 if ((Input.GetKey(KeyCode.LeftShift) | Input.GetKey(KeyCode.RightShift)) && isMoving)
376 {
377 targetSpeed *= runSpeed;
378 _characterState = PickupCharacterState.Running;
379 }
380 else if (Time.time - trotAfterSeconds > walkTimeStart)
381 {
382 targetSpeed *= trotSpeed;
383 _characterState = PickupCharacterState.Trotting;
384 }
385 else if (isMoving)
386 {
387 targetSpeed *= walkSpeed;
388 _characterState = PickupCharacterState.Walking;
389 }
390
391 moveSpeed = Mathf.Lerp(moveSpeed, targetSpeed, curSmooth);
392
393 // Reset walk time start when we slow down
394 if (moveSpeed < walkSpeed * 0.3f)
395 walkTimeStart = Time.time;
396 }
397 // In air controls
398 else
399 {
400 // Lock camera while in air
401 if (jumping)
402 lockCameraTimer = 0.0f;
403
404 if (isMoving)
405 inAirVelocity += targetDirection.normalized * Time.deltaTime * inAirControlAcceleration;
406 }
407 }
File name: ThirdPersonCamera.cs
Copy
86 void Apply( Transform dummyTarget, Vector3 dummyCenter )
87 {
88 // Early out if we don't have a target
89 if( !controller )
90 return;
91
92 Vector3 targetCenter = _target.position + centerOffset;
93 Vector3 targetHead = _target.position + headOffset;
94
95 // DebugDrawStuff();
96
97 // Calculate the current & target rotation angles
98 float originalTargetAngle = _target.eulerAngles.y;
99 float currentAngle = cameraTransform.eulerAngles.y;
100
101 // Adjust real target angle when camera is locked
102 float targetAngle = originalTargetAngle;
103
104 // When pressing Fire2 (alt) the camera will snap to the target direction real quick.
105 // It will stop snapping when it reaches the target
106 if( Input.GetButton( "Fire2" ) )
107 snap = true;
108
109 if( snap )
110 {
111 // We are close to the target, so we can stop snapping now!
112 if( AngleDistance( currentAngle, originalTargetAngle ) < 3.0f )
113 snap = false;
114
115 currentAngle = Mathf.SmoothDampAngle( currentAngle, targetAngle, ref angleVelocity, snapSmoothLag, snapMaxSpeed );
116 }
117 // Normal camera motion
118 else
119 {
120 if( controller.GetLockCameraTimer() < lockCameraTimeout )
121 {
122 targetAngle = currentAngle;
123 }
124
125 // Lock the camera when moving backwards!
126 // * It is really confusing to do 180 degree spins when turning around.
127 if( AngleDistance( currentAngle, targetAngle ) > 160 && controller.IsMovingBackwards() )
128 targetAngle += 180;
129
130 currentAngle = Mathf.SmoothDampAngle( currentAngle, targetAngle, ref angleVelocity, angularSmoothLag, angularMaxSpeed );
131 }
132
133
134 // When jumping don't move camera upwards but only down!
135 if( controller.IsJumping() )
136 {
137 // We'd be moving the camera upwards, do that only if it's really high
138 float newTargetHeight = targetCenter.y + height;
139 if( newTargetHeight < targetHeight || newTargetHeight - targetHeight > 5 )
140 targetHeight = targetCenter.y + height;
141 }
142 // When walking always update the target height
143 else
144 {
145 targetHeight = targetCenter.y + height;
146 }
147
148 // Damp the height
149 float currentHeight = cameraTransform.position.y;
150 currentHeight = Mathf.SmoothDamp( currentHeight, targetHeight, ref heightVelocity, heightSmoothLag );
151
152 // Convert the angle into a rotation, by which we then reposition the camera
153 Quaternion currentRotation = Quaternion.Euler( 0, currentAngle, 0 );
154
155 // Set the position of the camera on the x-z plane to:
156 // distance meters behind the target
157 cameraTransform.position = targetCenter;
158 cameraTransform.position += currentRotation * Vector3.back * distance;
159
160 // Set the height of the camera
161 cameraTransform.position = new Vector3( cameraTransform.position.x, currentHeight, cameraTransform.position.z );
162
163 // Always look at the target
164 SetUpRotation( targetCenter, targetHead );
165 }
File name: ThirdPersonController.cs
Copy
131 void UpdateSmoothedMovementDirection()
132 {
133 Transform cameraTransform = Camera.main.transform;
134 bool grounded = IsGrounded();
135
136 // Forward vector relative to the camera along the x-z plane
137 Vector3 forward = cameraTransform.TransformDirection(Vector3.forward);
138 forward.y = 0;
139 forward = forward.normalized;
140
141 // Right vector relative to the camera
142 // Always orthogonal to the forward vector
143 Vector3 right = new Vector3(forward.z, 0, -forward.x);
144
145 float v = Input.GetAxisRaw("Vertical");
146 float h = Input.GetAxisRaw("Horizontal");
147
148 // Are we moving backwards or looking backwards
149 if (v < -0.2f)
150 movingBack = true;
151 else
152 movingBack = false;
153
154 bool wasMoving = isMoving;
155 isMoving = Mathf.Abs(h) > 0.1f || Mathf.Abs(v) > 0.1f;
156
157 // Target direction relative to the camera
158 Vector3 targetDirection = h * right + v * forward;
159
160 // Grounded controls
161 if (grounded)
162 {
163 // Lock camera for short period when transitioning moving & standing still
164 lockCameraTimer += Time.deltaTime;
165 if (isMoving != wasMoving)
166 lockCameraTimer = 0.0f;
167
168 // We store speed and direction seperately,
169 // so that when the character stands still we still have a valid forward direction
170 // moveDirection is always normalized, and we only update it if there is user input.
171 if (targetDirection != Vector3.zero)
172 {
173 // If we are really slow, just snap to the target direction
174 if (moveSpeed < walkSpeed * 0.9f && grounded)
175 {
176 moveDirection = targetDirection.normalized;
177 }
178 // Otherwise smoothly turn towards it
179 else
180 {
181 moveDirection = Vector3.RotateTowards(moveDirection, targetDirection, rotateSpeed * Mathf.Deg2Rad * Time.deltaTime, 1000);
182
183 moveDirection = moveDirection.normalized;
184 }
185 }
186
187 // Smooth the speed based on the current target direction
188 float curSmooth = speedSmoothing * Time.deltaTime;
189
190 // Choose target speed
191 //* We want to support analog input but make sure you cant walk faster diagonally than just forward or sideways
192 float targetSpeed = Mathf.Min(targetDirection.magnitude, 1.0f);
193
194 _characterState = CharacterState.Idle;
195
196 // Pick speed modifier
197 if (Input.GetKey(KeyCode.LeftShift) | Input.GetKey(KeyCode.RightShift))
198 {
199 targetSpeed *= runSpeed;
200 _characterState = CharacterState.Running;
201 }
202 else if (Time.time - trotAfterSeconds > walkTimeStart)
203 {
204 targetSpeed *= trotSpeed;
205 _characterState = CharacterState.Trotting;
206 }
207 else
208 {
209 targetSpeed *= walkSpeed;
210 _characterState = CharacterState.Walking;
211 }
212
213 moveSpeed = Mathf.Lerp(moveSpeed, targetSpeed, curSmooth);
214
215 // Reset walk time start when we slow down
216 if (moveSpeed < walkSpeed * 0.3f)
217 walkTimeStart = Time.time;
218 }
219 // In air controls
220 else
221 {
222 // Lock camera while in air
223 if (jumping)
224 lockCameraTimer = 0.0f;
225
226 if (isMoving)
227 inAirVelocity += targetDirection.normalized * Time.deltaTime * inAirControlAcceleration;
228 }
229
230
231
232 }
File name: ThirdPersonController.cs
Copy
451 public float GetLockCameraTimer()
452 {
453 return lockCameraTimer;
454 }
Download file with original file name:Timer
Timer 222 lượt xem
Gõ tìm kiếm nhanh...