Search Unity

  1. We've closed the job boards. If you're looking for work, or looking to hire check out Unity Connect. You can see more information here.
    Dismiss Notice
  2. We're running great holiday deals on subscriptions, swag and Asset Store packages! Take a peek at this blog for more information!
    Dismiss Notice
  3. Check out our Unite Austin 2017 YouTube playlist to catch up on what you missed. More videos coming soon.
    Dismiss Notice
  4. Unity 2017.2 is now released.
    Dismiss Notice
  5. The Unity Gear Store is here to help you look great at your next meetup, user group or conference. With all new Unity apparel, stickers and more!
    Dismiss Notice
  6. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  7. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice
  8. Unity 2017.3 beta is now available for download.
    Dismiss Notice

Collision detection (2d) with clone

Discussion in 'Physics' started by tiremo, Jun 19, 2017.

  1. tiremo

    tiremo

    Joined:
    Jun 13, 2017
    Posts:
    1
    Hello,

    I struggle a few days now with this problem. In my Player script, the sphere gets instatiatet everytime I press the spacebar. This works pretty good.
    And now, when I shoot another one, I want to check the collision between them. They have both a property called "sphereVal" and the value is random.
    The problem now is, when they collide, both of them trigger the OnCollisionEnter2D.

    Lets say the first sphere fired has the value 10 and the other one 20. When 10 hits 20 it Outputs "less" and "greater" at the same time because they "looking for each other".

    I think ist because they have the same GameTag but I have no clue how to deal with that.

    My goal is, when I shoot a sphere, it should only trigger the collision from "one perspective", if that makes sense.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class sphere: MonoBehaviour {
    6.     public Rigidbody2D rb;
    7.     public int speed;
    8.     public int sphereVal;
    9.     // Use this for initialization
    10.     void Start () {
    11.         rb = GetComponent<Rigidbody2D>();
    12.         rb.tag = "sphere";
    13.         kugelZahl = Random.Range(1,100);
    14.         speed = 6;
    15.        
    16.     }
    17.    
    18.     // Update is called once per frame
    19.     void Update () {
    20.             float moveVertical = Input.GetAxisRaw("Vertical");
    21.             Vector3 toTop = new Vector3(0, moveVertical + 0.5f, 0.0f);
    22.             rb.velocity = toTop * speed;
    23.     }
    24.  
    25.     private void OnCollisionEnter2D(Collision2D collision)
    26.     {
    27.        
    28.         if (collision.gameObject.tag == "sphere" || collision.gameObject.tag == "background")
    29.         {
    30.            
    31.            
    32.             rb.constraints = RigidbodyConstraints2D.FreezePositionY | RigidbodyConstraints2D.FreezePositionX;
    33.             int a = collision.gameObject.GetComponent<kugel>().kugelZahl;
    34.            
    35.             if(sphereVal< collision.gameObject.GetComponent<kugel>().sphereVal)
    36.             {
    37.                
    38.                 Debug.Log("less");
    39.             }
    40.             if (sphereVal> collision.gameObject.GetComponent<sphere>().sphereVal)
    41.             {
    42.                 Debug.Log("greater");
    43.             }
    44.  
    45.         }
    46.     }
    47. }
    Greetings from Germany! (Yep, this explains my english)
     
  2. SiliconDroid

    SiliconDroid

    Joined:
    Feb 20, 2017
    Posts:
    257
    Hi,

    Your English is more than good enough.

    Even with unique tags both OnCollisionEnter2D handlers will fire.

    If you must have all spheres listening and you want to keep all your "sphere" scripts simple and symmetrical then you could tackle this by making a simple global collision manager; a simple singleton class (only one instance of this class exists for whole game):

    That singleton should receives a call like:

    Code (CSharp):
    1. AddCollision(Collision2D collision)
    So it will still be called twice for each collision between 2 objects.

    But that manager can make decisions based on filters:

    The manager maintains a short cyclic buffer of previous collisions and their times.

    The manager only acts once on collisions <K distance apart.

    The manager only acts once on collisions <T time apart.

    The manager can also make higher level logic decisions (i.e. higher balls kill lower balls etc).

    You may want to pass more info into the manager like:

    Code (CSharp):
    1. AddCollision(Collision2D collision, sphere cSphere)
    Then the manager can also read any public data in cSphere/s for advanced decision making.
     
    Last edited: Jun 28, 2017