Ejemplo n.º 1
0
        /// <summary>
        /// Checks for any collisions between the camera and the target, calculating a new Z axis point that will not be obstructed by the cause of the collision.
        /// </summary>
        private void CameraCollision(float targetZ, ref float actualZ)
        {
            StateManager states        = FindObjectOfType <StateManager>(); // Player reference
            float        step          = Mathf.Abs(targetZ);
            int          stepCount     = 1;
            float        stepIncrement = step / stepCount;

            RaycastHit hit;
            Vector3    origin    = pivotPoint.position;
            Vector3    direction = -pivotPoint.forward;

            //Debug.DrawRay(origin, direction * step, Color.blue);
            if (Physics.Raycast(origin, direction, out hit, step, cameraCollisionLayers)) // Raycast, ignoring the same layers as the player
            {
                if (!hit.transform.GetComponent <Enemy>())                                // If the raycast returns an object which is not a player or enemy
                {
                    //Debug.Log(hit.transform.root.name);
                    float distance = Vector3.Distance(hit.point, origin); // Calculate distance from the point hit and the origin
                    actualZ = -(distance / 2);                            // Halve the distance and convert to negative value
                }
                //Debug.Log(hit.transform.name + " was hit!");
            }
            else // If nothing is hit by the raycast...
            {
                //print("Nothing hit");
                for (int s = 0; s < stepCount + 1; s++)
                {
                    for (int i = 0; i < 4; i++) // Loop 4 times to raycast out in 4 different directions to check for additional obstacles to the camera
                    {
                        Vector3 dir          = Vector3.zero;
                        Vector3 secondOrigin = origin + (direction * s) * stepIncrement;

                        switch (i)
                        {
                        case 0:
                            dir = cameraTransform.right;
                            break;

                        case 1:
                            dir = -cameraTransform.right;
                            break;

                        case 2:
                            dir = cameraTransform.up;
                            break;

                        case 3:
                            dir = -cameraTransform.up;
                            break;
                        }

                        //Debug.DrawRay(secondOrigin, dir * 0.5f, Color.red);

                        if (Physics.Raycast(secondOrigin, dir, out hit, 0.5f, cameraCollisionLayers)) // Raycast, ignoring the same layers as the player
                        {
                            if (!hit.transform.GetComponent <Enemy>())                                // If the raycast returns an object which is not a player or enemy
                            {
                                //Debug.Log(hit.transform.root.name);
                                float distance = Vector3.Distance(secondOrigin, origin); // Calculate distance from both origins
                                actualZ = -(distance / 2);                               // Halve the distance and convert to negative value

                                if (actualZ < 0.2f)
                                {
                                    actualZ = 0;
                                }
                                return;
                            }
                        }
                    }
                }
            }
        }