RoundToInt
How do I use Round To Int
Below are practical examples compiled from projects for learning and reference purposes
Featured Snippets
File name: Grid.cs
Copy
68 public Node GetNodeAt(Vector3 pos) {
69 pos -= transform.position;
70
71 float percentRow = (pos.z / size.z) + 0.5f;
72 float percentCol = (pos.x / size.x) + 0.5f;
73 percentRow = Mathf.Clamp01(percentRow);
74 percentCol = Mathf.Clamp01(percentCol);
75 int row = Mathf.RoundToInt((rows-1) * percentRow);
76 int col = Mathf.RoundToInt((cols-1) * percentCol);
77
78 return grid[row,col];
79 }
File name: SimpleFPSCounter.cs
Copy
13 void Update()
14 {
15 if (!active) return;
16
17 numFrames++;
18 elapsedTime += Time.deltaTime;
19 if (elapsedTime >= 1f)
20 {
21 fps = numFrames / elapsedTime;
22 numFrames = 0;
23 elapsedTime = 0;
24 }
25 print(Mathf.RoundToInt(fps));
26 }
File name: PlayerMoveScript.cs
Copy
98 void OnCollisionEnter2D(Collision2D target) {
99 if(target.gameObject.tag == "died" || target.gameObject.tag == "Crates") {
100 jumpForce = 0;
101 scoreCount = 0;
102 anim.SetTrigger ("Died");
103 scoreText.gameObject.SetActive (false);
104 audioSource.PlayOneShot (diedClip);
105 FindObjectOfType
106 FindObjectOfType
107 FindObjectOfType
108 }
109 }
File name: EnemyController.cs
Copy
43 void OnCollisionEnter2D(Collision2D collision){
44 if(collision.relativeVelocity.magnitude > damageCounter){
45 hitPoints -= Mathf.RoundToInt(collision.relativeVelocity.magnitude);
46 UpdateScoreStatus (Mathf.RoundToInt (collision.relativeVelocity.magnitude));
47 if(GameController.instance != null && MusicController.instance != null){
48 if(GameController.instance.isMusicOn){
49 if (gameObject != null) {
50 AudioSource.PlayClipAtPoint (hurt, transform.position);
51 }
52 }
53 }
54 }
55
56
57 UpdateAnimationState ();
58
59 if(hitPoints <= 0){
60 Death ();
61
62 if(collision.gameObject.CompareTag("Player Bullet")){
63 bounce = collision.transform.GetComponent
64 bounce.y = 0f;
65 collision.transform.GetComponent
66
67 }
68 }
69 }
File name: Structure.cs
Copy
46 void OnCollisionEnter2D(Collision2D collision){
47 if(collision.relativeVelocity.magnitude > damageCounter){
48 hitpoints -= Mathf.RoundToInt (collision.relativeVelocity.magnitude);
49 UpdateScoreStatus (Mathf.RoundToInt (collision.relativeVelocity.magnitude));
50 }
51
52
53 if (hitpoints <= 50) {
54 spriteRenderer.sprite = sprite [0];
55 if(counter == 2){
56 AudioManager ();
57 counter--;
58 }
59
60 }
61
62 if(hitpoints <= 30){
63 spriteRenderer.sprite = sprite [1];
64 if(counter == 1){
65 AudioManager ();
66 counter--;
67 }
68 }
69
70
71 if(hitpoints <= 0){
72 Destroyed ();
73
74 if(collision.gameObject.CompareTag("Player Bullet")){
75 bounce = collision.transform.GetComponent
76 bounce.y = 0f;
77 collision.transform.GetComponent
78 }
79 }
80 }
File name: ScrollSnap.cs
Copy
429 public int CurrentPage()
430 {
431 float pos;
432
433 if (direction == ScrollDirection.Horizontal)
434 {
435 pos = listContainerMaxPosition - listContainerTransform.localPosition.x;
436 pos = Mathf.Clamp(pos, 0, listContainerSize);
437 }
438 else
439 {
440 pos = listContainerTransform.localPosition.y - listContainerMinPosition;
441 pos = Mathf.Clamp(pos, 0, listContainerSize);
442 }
443
444 float page = pos / itemSize;
445
446 return Mathf.Clamp(Mathf.RoundToInt(page), 0, pages);
447 }
RoundToInt 157 lượt xem
Gõ tìm kiếm nhanh...