Collision2D









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

Featured Snippets


File name: BallControl.cs Copy
36  void OnCollisionEnter2D(Collision2D coll) {
37   if (coll.collider.CompareTag ("Player")) {
38    vel.x = rb2d.velocity.x;
39    vel.y = (rb2d.velocity.y / 2.0f) + (coll.collider.attachedRigidbody.velocity.y / 3.0f);
40    rb2d.velocity = vel;
41   }
42  }
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 ().gameOver (Mathf.RoundToInt(highScoreCount), coinScore);
106    FindObjectOfType ().ifPlayerDiedCoinScore(coinScore);
107    FindObjectOfType ().ifPlayerDiedScore (Mathf.RoundToInt(highScoreCount));
108   }
109  }
File name: PlayerBulletCtrl.cs Copy
20     private void OnCollisionEnter2D(Collision2D collision)
21     {
22         if (collision.gameObject.CompareTag("Enemy"))
23         {
24             GameCtrl.instance.BulletHitEnemy(collision.gameObject.transform);
25             Destroy(gameObject);
26         }
27         else if (collision.gameObject.CompareTag("Player"))
28         {
29             Destroy(gameObject);
30         }
31     }
File name: PlayerCtrl.cs Copy
168     void OnCollisionEnter2D(Collision2D other)
169     {
170         if (other.gameObject.CompareTag("Ground"))
171         {
172             isJumping = false;
173         }
174
175         if (other.gameObject.CompareTag("Enemy"))
176         {
177             Destroy(gameObject);
178         }
179     }
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 ().velocity;
64     bounce.y = 0f;
65     collision.transform.GetComponent ().velocity = bounce;
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 ().velocity;
76     bounce.y = 0f;
77     collision.transform.GetComponent ().velocity = bounce;
78    }
79   }
80  }
File name: BlockScript.cs Copy
23     void OnCollisionExit2D(Collision2D col)
24     {
25         gameObject.SetActive(false);
26         GameManager.Score += 20;
27         GameManager.BlocksAlive--;
28     }
File name: FloorScript.cs Copy
8     void OnCollisionEnter2D(Collision2D col)
9     {
10         if (col.gameObject.tag == "Ball")
11             gameManager.DecreaseLives();
12     }
File name: Animal.cs Copy
300         public void OnCollisionEnter2D(Collision2D otherCollision)
301         {
302             if (otherCollision.gameObject.name == MapObjectNames.GroundObject)
303             {
304                 state = RUN;
305                 stepJump = 2;
306                 isSprings = false;
307             }
308             else if (otherCollision.gameObject.name == "Bullet")
309             {
310                 if (gameObject.GetComponent().IsProtected || gameObject.GetComponent().isStanding) return;
311                 if (animalIndex != otherCollision.gameObject.GetComponent().animalIndex)
312                 {
313                     if (otherCollision.gameObject.GetComponent().skillType == SkillType.THIENTHACH || otherCollision.gameObject.GetComponent().skillType == SkillType.BOM)
314                     {
315                         if (otherCollision.gameObject.GetComponent().animalIndex == 0)
316                         {
317                             gameScreen.addScore(10, gameObject.transform.localPosition);
318                             gameScreen.setCombo(1, otherCollision.gameObject.GetComponent().skillType);
319                         }
320                         gameScreen.AnimalcollisionWithBullet(gameObject, otherCollision.gameObject);
321                     }
322                 }
323             }
324         }
File name: Animal.cs Copy
419         public void OnCollisionExit2D(Collision2D otherCollision)
420         {
421             if (otherCollision.gameObject.name == MapObjectNames.GroundObject)
422             {
423                 if (state == RUN)
424                 {
425                     state = JUMPING;
426                 }
427             }
428         }

Collision2D 122 lượt xem

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