Example #1
0
    /// <summary>
    /// Custom Raycast using NearClipPlanesPoints
    /// </summary>
    /// <param name="_to"></param>
    /// <param name="from"></param>
    /// <param name="hitInfo"></param>
    /// <param name="distance"></param>
    /// <param name="cullingLayer"></param>
    /// <returns></returns>

    bool CullingRayCast(Vector3 from, ClipPlanePointsFunctions.ClipPlanePoints _to, out RaycastHit hitInfo, float distance, LayerMask cullingLayer, Color color)
    {
        bool value = false;

        if (Physics.Raycast(from, _to.LowerLeft - from, out hitInfo, distance, cullingLayer))
        {
            value           = true;
            cullingDistance = hitInfo.distance;
        }

        if (Physics.Raycast(from, _to.LowerRight - from, out hitInfo, distance, cullingLayer))
        {
            value = true;
            if (cullingDistance > hitInfo.distance)
            {
                cullingDistance = hitInfo.distance;
            }
        }

        if (Physics.Raycast(from, _to.UpperLeft - from, out hitInfo, distance, cullingLayer))
        {
            value = true;
            if (cullingDistance > hitInfo.distance)
            {
                cullingDistance = hitInfo.distance;
            }
        }

        if (Physics.Raycast(from, _to.UpperRight - from, out hitInfo, distance, cullingLayer))
        {
            value = true;
            if (cullingDistance > hitInfo.distance)
            {
                cullingDistance = hitInfo.distance;
            }
        }

        return(value);
    }
Example #2
0
    /// <summary>
    /// Camera behaviour
    /// </summary>
    void CameraMovement()
    {
        if (currentTarget == null)
        {
            return;
        }

        distance = Mathf.Lerp(distance, defaultDistance, smoothFollow * Time.deltaTime);
        //_camera.fieldOfView = fov;
        cullingDistance = Mathf.Lerp(cullingDistance, distance, Time.deltaTime);
        var camDir = (forward * targetLookAt.forward) + (rightOffset * targetLookAt.right);

        camDir = camDir.normalized;

        var targetPos = new Vector3(currentTarget.position.x, currentTarget.position.y + offSetPlayerPivot, currentTarget.position.z);

        currentTargetPos = targetPos;
        desired_cPos     = targetPos + new Vector3(0, height, 0);
        current_cPos     = currentTargetPos + new Vector3(0, currentHeight, 0);
        RaycastHit hitInfo;

        ClipPlanePointsFunctions.ClipPlanePoints planePoints = ClipPlanePointsFunctions.NearClipPlanePoints(_camera, current_cPos + (camDir * (distance)), clipPlaneMargin);
        ClipPlanePointsFunctions.ClipPlanePoints oldPoints   = ClipPlanePointsFunctions.NearClipPlanePoints(_camera, desired_cPos + (camDir * distance), clipPlaneMargin);

        //Check if Height is not blocked
        if (Physics.SphereCast(targetPos, checkHeightRadius, Vector3.up, out hitInfo, cullingHeight + 0.2f, cullingLayer))
        {
            var t = hitInfo.distance - 0.2f;
            t            -= height;
            t            /= (cullingHeight - height);
            cullingHeight = Mathf.Lerp(height, cullingHeight, Mathf.Clamp(t, 0.0f, 1.0f));
        }

        //Check if desired target position is not blocked
        if (CullingRayCast(desired_cPos, oldPoints, out hitInfo, distance + 0.2f, cullingLayer, Color.blue))
        {
            distance = hitInfo.distance - 0.2f;
            if (distance < defaultDistance)
            {
                var t = hitInfo.distance;
                t            -= cullingMinDist;
                t            /= cullingMinDist;
                currentHeight = Mathf.Lerp(cullingHeight, height, Mathf.Clamp(t, 0.0f, 1.0f));
                current_cPos  = currentTargetPos + new Vector3(0, currentHeight, 0);
            }
        }
        else
        {
            currentHeight = height;
        }
        //Check if target position with culling height applied is not blocked
        if (CullingRayCast(current_cPos, planePoints, out hitInfo, distance, cullingLayer, Color.cyan))
        {
            distance = Mathf.Clamp(cullingDistance, 0.0f, defaultDistance);
        }
        var lookPoint = current_cPos + targetLookAt.forward * 2f;

        lookPoint            += (targetLookAt.right * Vector3.Dot(camDir * (distance), targetLookAt.right));
        targetLookAt.position = current_cPos;

        transform.position = current_cPos + (camDir * (distance));

        Quaternion newRot;

        if (!lockCamera)
        {
            newRot = Quaternion.Euler(mouseY, mouseX, 0);
        }
        else
        {
            newRot = Quaternion.Euler(XValue, mouseX, 0);
        }

        targetLookAt.rotation = Quaternion.Slerp(targetLookAt.rotation, newRot, smoothCameraRotation * Time.deltaTime);
        var rotation = Quaternion.LookRotation((lookPoint) - transform.position);

        transform.rotation = rotation;

        //lookTargetOffSet = Vector3.Lerp(lookTargetOffSet, Vector3.zero, 1 * Time.fixedDeltaTime);

        //rotation.eulerAngles += rotationOffSet + lookTargetOffSet;
        movementSpeed = Vector2.zero;
    }