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

What is the syntax for this command? (list)

Discussion in 'Scripting' started by Marscaleb, Sep 4, 2019.

  1. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    996
    I am trying to retrieve a reference to the gameobject currently touching my player, specifically the ground the player is standing on. I have a reference in my code to the collider2D that touches the ground, and I'm looking for the best way to use that collider2D to get a reference to to the ground's gameobject.
    (Using OnCollisionEnter wouldn't be advisable because it would be too complicated to make sure I am not getting a message from something else that touched the player.)

    What looks like my best option is to use Collider2D.overlapCollider or Collider2D.Raycast to find the object touching my collider, and the former looks to be the more efficient choice.
    https://docs.unity3d.com/ScriptReference/Collider2D.OverlapCollider.html
    However, I do not understand the proper syntax I need to use to get a reference to anything found in "results."
    Could someone please explain this? Show an example?
    If multiple colliders are touching (there should never be more than two) I honestly don't care which one it grabs a reference to.
     
  2. jc_crash

    jc_crash

    Joined:
    Jul 30, 2019
    Posts:
    21
    Hi @Marscaleb

    Why exactly is using OnCollisionEnter too complicated? You can see in the collision data which GameObject you are colliding with. You can tag your ground (or whatever prefab/gameobject) with a unique tag (say, "ground"), and compare the tag in OnCollisionEnter. Something like:

    void OnCollisionEnter(Collision collision) {
    if (collision.gameObject.CompareTag("ground")) {
    // do your thing here
    }
    }
     
  3. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,386
    It's just an array of colliders. Are you you having trouble with the array syntax, or the collider class?
     
  4. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    996
    Sorry, I see the detail I omitted.
    My player has multiple colliders, and I'm working with just the specific colliders that would touch the ground. If I use OnCollisionEnter, I bypass that level of specificity. If I have the ground try to reach out to the player when it gets touched, how am I supposed to know if the player is actually standing on the ground, and didn't just bump it from the side?
    If I use the OnCollisionEnter inside the player, then I'm going to be calling this code A LOT, which is terribly inefficient, and then I still come to the same issue of "how do I know this is something the player is standing on and not something they just bumped into?"
    Which is why I want to work through a Collider2D, because I already have a reference to the colliders on the bottom of the player, so if I'm checking *that specific collider* then I know whatever it is touching is what the player is standing on.

    Although having said that, I'm starting to re-think my process. I was trying to run this code to check what the player is on every time the player lands, but it is only in special circumstances where that knowledge actually matters. I'm thinking it would be smarter to just put the code in the ground-object whenever it is abnormal ground, and then that script can just call a function in the player to tell the player about the special ground.
    But that still leaves me with the question of how I can make sure the object knows the player is standing on it, and not just bumping it from the side.
    I guess the ground could tell the player what it's height is, and the player could compare that to its own Y position and ignore anything that isn't below the player's feet. But then things could get screwed up if the player can manage to bump the ground from the side and then stand on it without losing contact with the ground. I don't know how that could ever happen, but I'm still open to any ideas for reasonable ways to check if the player is standing on a surface or not.


    I suppose the array syntax? If it is storing this data as an array, how can I retrieve that data? Am I supposed to create a new array to copy that array's data to, just so I can get one item within that array? That sounds... wasteful. And even then I am unsure how I would write that out.
     
  5. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,386
    You don't copy anything. The function doesn't actually create an array. Instead it fills the array that you give it. It would look like this:
    Code (csharp):
    1.  
    2.         //I create the array
    3.         Collider2D[] myResults = new Collider2D[10];
    4.         // This function fills the array with values
    5.         int numberOfResults =myCollider.OverlapCollider(myContactFilter, myResults);
    6.        
    7.         for (int i=0;i< numberOfResults;i++)
    8.         {
    9.            Debug.Log(myResults[i].gameObject.name);
    10.         }
    11.  
    It's sort of crude, but the point is that it doesn't need to allocate a new array every time. Note that in this case (with the array size of 10) if the collider is only colliding with 4 other colliders then that means that the last 6 entries of the array will be invalid results. Just ignore them. Likewise, if the collider collides with 12 other colliders than 2 of those will by missing from your results entirely.
     
  6. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    996
    Ah, that's the syntax I was looking for. Thank you, kdgalla.