public void checkCollisionable(GameObject g)
    {
        CustomCollisionProperties otherProp = g.GetComponent <CustomCollisionProperties>();
        Collider2D oc = g.GetComponent <Collider2D>();

        if (oc == null || otherProp == null)
        {
            return;
        }


        if (otherProp != null)
        {
            //Debug.Log("Checking: " + oc.gameObject + " from script: " + otherProp);
            if (physicCollisions.Contains(otherProp.getObjectType()) || otherProp.physicCollisions.Contains(getObjectType()))
            {
                //OnCollision();
            }
            else
            {
                Physics2D.IgnoreCollision(selfCollider, oc);
            }
        }
        else
        {
            //Physics2D.IgnoreCollision(selfCollider, oc);
        }
    }
Esempio n. 2
0
    // If the magic enters in contact with another collider
    private void OnTriggerEnter2D(Collider2D collider)
    {
        // Check if the other object has a "CustomCollisionProperties" script.
        CustomCollisionProperties otherProp = collider.gameObject.GetComponent <CustomCollisionProperties>();

        Debug.Log("La magia colisiona con: " + otherProp.gameObject);
        if (otherProp != null)
        {
            if (collidableTypes.Contains(otherProp.getObjectType()) && !consumed)
            {
                consumed = true;
                OnCollision();
                // Tell the other gameobject to apply any magic effect against them.
                applyMagicEffect(collider);
            }
            else
            {
                Physics2D.IgnoreCollision(selfCollider, collider);
            }
        }
        else
        {
            Physics2D.IgnoreCollision(selfCollider, collider);
        }
    }
    private bool isGrounded()
    {
        // Distance to the base of the collider from the pivot point (we don't want to use the centre).
        float distToColliderBase = col.bounds.extents.y;
        // Ray start position begin on the feet to make it clear (with a certain offset so it's not completely the base).
        Vector3 rayStartPosition = new Vector3(transform.position.x, transform.position.y - distToColliderBase + groundRayVerticalOffset, transform.position.z);

        Debug.DrawRay(rayStartPosition, new Vector3(0, -(groundRayLength), 0), Color.yellow);
        RaycastHit2D[] r = Physics2D.RaycastAll(rayStartPosition, new Vector3(0, -groundRayLength, 0), groundRayLength);

        //Debug.Log("Distancia actual de colisión: " + r.distance + " contra: " + r.collider.name);
        // Nothing to raycast?
        if (r.Length == 0)
        {
            return(false);
        }
        // Is the other object ground?
        bool                otherIsGround = false;
        RaycastHit2D        terrainHit    = r[0];
        List <RaycastHit2D> candidates    = new List <RaycastHit2D>();

        foreach (RaycastHit2D hit in r)
        {
            //Debug.Log("Has collided against: " + hit.collider.gameObject.name);
            CustomCollisionProperties ccp = hit.collider.gameObject.GetComponent <CustomCollisionProperties>();
            //Debug.Log("ccp: " + ccp);
            if (ccp != null && ccp.ObjectType == CustomCollisionProperties.ObjectTypeEnum.Terrain)
            {
                otherIsGround = true;
                candidates.Add(hit);
            }
        }

        // Get first one as default.
        if (candidates.Count != 0)
        {
            terrainHit = candidates[0];
        }

        // Iterate over all candidates and select the nearest
        foreach (RaycastHit2D cand in candidates)
        {
            if (terrainHit.distance > cand.distance)
            {
                terrainHit = cand;
            }
        }

        if (!otherIsGround)
        {
            return(false);
        }

        // We will return true if the distance to the ray is less than the double of its offset.
        if (terrainHit.collider != null && terrainHit.distance < (groundRayVerticalOffset * 2) && otherIsGround)
        {
            return(true);
        }
        else
        {
            return(false);
        }
    }