Example #1
0
    void LateUpdate()
    {
        if (Game.GetPlayer() == null)
        {
            return;
        }

        Entity controllableEntity = Game.GetPlayer().ControllableEntity;

        if (controllableEntity == null)
        {
            return;
        }

        if (Input.GetMouseButton(1))
        {
            actualCameraRotation.x += Input.GetAxis("Mouse X") * sensitivity.x * 0.02f;
            actualCameraRotation.y -= Input.GetAxis("Mouse Y") * sensitivity.y * 0.02f;
            desiredDistance        -= Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * zoomRate * Mathf.Abs(desiredDistance);
        }

        actualCameraRotation.y = MathX.ClampAngle(actualCameraRotation.y, rotationLimit.x, rotationLimit.y);
        desiredDistance        = Mathf.Clamp(desiredDistance, minDistance, maxDistance);
        correctedDistance      = desiredDistance;
        Quaternion rotation = Quaternion.Euler(actualCameraRotation.y, actualCameraRotation.x, 0);
        RaycastHit hit;

        Vector3 position  = controllableEntity.transform.position - (rotation * Vector3.forward * desiredDistance - vTargetOffset);
        Vector3 targetPos = controllableEntity.transform.position + vTargetOffset;

        if (Physics.Linecast(targetPos, position, out hit, raycastMask))
        {
            correctedDistance = Vector3.Distance(targetPos, hit.point);
        }

        position = controllableEntity.transform.position - (rotation * Vector3.forward * correctedDistance - vTargetOffset);

        transform.position = Vector3.Lerp(transform.position, position, 0.5f);
        transform.rotation = Quaternion.Lerp(transform.rotation, rotation, 0.5f);
    }