Trying to understand a script

Updated on January 22, 2019 in Unity
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
2 on January 22, 2019

Hello guys, im a new programmer trying to understand one of brackeys script.

the script is the “CharacterController2D”.

Collider2D[ ] colliders = Physics2D.OverlapCircleAll(m_GroundCheck.position, k_GroundedRadius, m_WhatIsGround);
        for (int i = 0; i < colliders.Length; i++)
        {
            if (colliders[i].gameObject != gameObject)
            {
                m_Grounded = true;
                if (!wasGrounded && m_Rigidbody2D.velocity.y < 0)
                    OnLandEvent.Invoke();

i dont know why the code check is game object is not equal to itself. Can anyone explain this to me? Thanks in advance

  • Liked by
Reply
1 on January 22, 2019

colliders is an array of colliders that they’re getting from an OverlapCircle function.

All of those colliders it finds has a gameObject associated with it. What the if statement is doing inside the for loop, is checking all of the colliders the OverlapCircle found, and it’s making sure they’re not this same object.

Basically, it’s just making sure that the thing it’s colliding with, is actually something different, and not just some loose part of the model, or something like that.

on January 22, 2019

awesome. thanks for your help. now it make sense. i thougth the objects was looking for itself or something. colliders[i].gameObject it referring to the game object within colliders and the != gameObject its the player (the game object asosiate to this script)

 

Show more replies
  • Liked by
Reply
Cancel