Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Make the object bounce on specific colliders

Discussion in 'Scripting' started by FalconJ, Mar 24, 2015.

  1. FalconJ

    FalconJ

    Joined:
    Mar 12, 2015
    Posts:
    146
    Is it possible to make the 2D object bounce on specific colliders only? Other than the specified colliders, the object will do another onTriggerEnter thing.
     
  2. Chris196

    Chris196

    Joined:
    Apr 16, 2015
    Posts:
    6
    i wanna know this too
     
  3. jerryjr261

    jerryjr261

    Joined:
    Jun 16, 2015
    Posts:
    31
    Assigning multiple colliders to one object and colliding with each of them independently.

    Hope this helps!

    Code (CSharp):
    1.  
    2.  
    3. BoxCollider2D[] colliderArray;
    4.  
    5.     void Start()
    6.     {
    7.         colliderArray = GetComponents<BoxCollider2D>();
    8.     }
    9.  
    10.     void OnCollisionEnter2D(Collision2D other)
    11.     {
    12.         if (colliderArray[0].IsTouching(other.collider))
    13.         {
    14.             // Kill player
    15.             Debug.Log("Kill Player");
    16.         }
    17.         else if (colliderArray[1].IsTouching(other.collider))
    18.         {
    19.             // Hit player
    20.             Debug.Log("Hit Player");
    21.         }
    22.         else if (colliderArray[2].IsTouching(other.collider))
    23.         {
    24.             // Heal player
    25.             Debug.Log("Heal Player");
    26.         }
    27.     }
    Another example:

    Code (CSharp):
    1.     BoxCollider2D[] colliderArray;
    2.  
    3.     void Start()
    4.     {
    5.         colliderArray = GetComponents<BoxCollider2D>();
    6.     }
    7.  
    8.     void OnCollisionEnter2D(Collision2D other)
    9.     {
    10.         if (colliderArray[0].IsTouching(other.collider) && other.collider.tag == "Player")
    11.         {
    12.             // Kill player
    13.             Debug.Log("Kill Player");
    14.         }
    15.         else if (colliderArray[1].IsTouching(other.collider) && other.collider.tag == "Player")
    16.         {
    17.             // Hit player
    18.             Debug.Log("Hit Player");
    19.         }
    20.         else if (colliderArray[2].IsTouching(other.collider) && other.collider.tag == "Player")
    21.         {
    22.             // Heal player
    23.             Debug.Log("Heal Player");
    24.         }
    25.     }
     
    Last edited: Oct 26, 2015
  4. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    You could also use tags on those objects then apply a bounce force on collision or on trigger by checking the tag