Exemple #1
0
    private GameObject SeeIfCollisionWithOtherObject(Vector3 RaycastDirection, int unitViewDistance, string tag)
    {
        float length = PlayGrid.getUnitWorldSize().x *unitViewDistance *Vector3.Magnitude(RaycastDirection);

        RaycastHit2D[] hit = Physics2D.RaycastAll(gameObject.transform.position, RaycastDirection.normalized, length);

        Debug.DrawLine(gameObject.transform.position, gameObject.transform.position + length * RaycastDirection.normalized, Color.blue, gameMaster.getRoundDuration());

        for (int i = 0; i < hit.Length; i++)
        {
            if (hit[i].collider.gameObject.tag == tag)
            {
                // Calculate the distance from the NPCreature to the next
                float distance = Vector3.Distance(hit[i].transform.position, transform.position);
                if (distance < 0.00001f)
                {
                    //Debug.Log("Raycast hits own collider (distance = " + distance + ")");
                }
                else
                {
                    //Debug.Log("NPCreature sees " + tag);
                    return(hit[i].collider.gameObject);
                }
            }
        }
        return(null);
    }