Esempio n. 1
0
    /// <summary>
    /// Sends raycast forward and checks if position is in the shadow of enemy and player
    /// </summary>
    private void TakeOverEntity()
    {
        RaycastHit hit;
        Vector3    rayOrigin = fpsCamera.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0));

        Debug.DrawRay(rayOrigin, fpsCamera.transform.forward * 1000.0f);

        if (Physics.Raycast(rayOrigin, fpsCamera.transform.forward, out hit, 10.0f, defaultLayer))
        {
            if (hit.collider.CompareTag("Gun"))
            {
                if (gunGameObject != null)
                {
                    gunGameObject.transform.parent = null;
                    gunGameObject.GetComponent <Rigidbody>().isKinematic = false;
                }

                gunGameObject = hit.collider.gameObject;
                gunGameObject.transform.parent        = GetComponentInChildren <Camera>().transform;
                gunGameObject.transform.localPosition = gunPosition.localPosition;
                gunGameObject.transform.rotation      = gunPosition.rotation;
                Gun = gunGameObject.GetComponent <Gun>();
                gunGameObject.GetComponent <Rigidbody>().isKinematic = true;

                if (Gun != null)
                {
                    UICanvas.AmmoInGun.text = Gun.amtOfBullets.ToString();
                    UICanvas.TotalAmmo.text = Gun.amtOfBulletsTotal.ToString();
                }
            }
        }

        else if (Physics.Raycast(rayOrigin, fpsCamera.transform.forward, out hit, 1000.0f, obstructionLayer))
        {
            if (hit.collider.CompareTag("Goal"))
            {
                GameObject shadowCaster            = LightManager.InShadow(hit.point, obstructionLayer);
                GameObject shadowCasterPlayerLight = InPlayerLightShadow(hit.point, obstructionLayer);
                if ((shadowCaster != null || shadowCasterPlayerLight != null) && (LightManager.InShadow(hit.point, playerLayer) || InPlayerLightShadow(hit.point, playerLayer)))
                {
                    if (shadowCaster == null)
                    {
                        shadowCaster = shadowCasterPlayerLight;
                    }

                    EnemyAI enemyAI = shadowCaster.GetComponentInParent <EnemyAI>();

                    //If the player is currently in control of an entity, leave that entity behind
                    if (ClaimedEntity != null)
                    {
                        ClaimedEntity.transform.parent = null;
                        ClaimedEntity.SetActive(true);
                    }

                    //Take control of the enemy whose shadow is overlapping with the players shadow
                    transform.position       = enemyAI.transform.position;
                    enemyAI.transform.parent = this.gameObject.transform;
                    ClaimedEntity            = enemyAI.gameObject;

                    enemyAI.gameObject.SetActive(false);
                }
            }
        }
    }