Esempio n. 1
0
    /// <summary>
    /// Shoot 3 rays and perform a damaging method on the first one to hit a proper target.
    /// </summary>
    public void GetRayTarget()
    {
        // Initialize a List of Vector3s to offset ray position.
        List <Vector3> directionList = new List <Vector3> {
            new Vector3(0, 1, 0), new Vector3(0, 0, 0), new Vector3(0, -1, 0)
        };

        RaycastHit rayHit;

        // Add one of the Vector3s to the original raycast Vector3 position to offset it in each round.
        // Call a damaging method on the object hit if found, and break immediately after the first hit.
        for (int i = 0; i <= directionList.Count; i++)
        {
            if (Physics.Raycast(player.transform.position + new Vector3(0, 0.5f, 0), player.transform.forward + directionList[i], out rayHit, range))
            {
                if (rayHit.transform.gameObject.CompareTag("Bear"))
                {
                    Bearcontroller bearTarget = rayHit.transform.GetComponent <Bearcontroller>();
                    Debug.Log(bearTarget.transform.name);
                    bearTarget.Damage(playerDamage);
                    break;
                }
                else if (rayHit.transform.gameObject.CompareTag("PLANT"))
                {
                    Plants plantTarget = rayHit.transform.GetComponent <Plants>();
                    Debug.Log(plantTarget.transform.name);
                    plantTarget.Damage(playerDamage);
                    break;
                }
                else if (rayHit.transform.gameObject.CompareTag("SNAKE"))
                {
                    Snake SnakeTarget = rayHit.transform.GetComponent <Snake>();
                    Debug.Log(SnakeTarget.transform.name);
                    SnakeTarget.Damage(playerDamage);
                    break;
                }
                else if (rayHit.transform.gameObject.CompareTag("WOLF"))
                {
                    Wolf WolfTarget = rayHit.transform.GetComponent <Wolf>();
                    Debug.Log(WolfTarget.transform.name);
                    WolfTarget.Damage(playerDamage);
                    break;
                }
            }
        }
    }