/* Anti camera clipping methods */ private void clipZoom() { clipZoomRayPlayerToCam.setEnd(camTarget.position); // Could use actual cam position instead of target, but this works more cleanly with more control clipZoomRayPlayerToCam.setDistance(maxCameraDistance); Vector3 direction = clipZoomRayPlayerToCam.getDirection().normalized; if (clipZoomRayPlayerToCam.castRay(out hitInfo, clipZoomLayer)) { //Add padding Vector3 newPosition = hitInfo.point - direction * clipZoomPaddingFactor * maxCameraDistance; //If either the padding sent me beyond the observed object OR if im too close to the object, resort to min distance Vector3 v = newPosition - observedObject.position; float minDistance = maxCameraDistance * clipZoomMinDistanceFactor; if (Vector3.Angle(v, direction) > 45f || Vector3.Distance(observedObject.position, newPosition) < minDistance) { newPosition = observedObject.position + direction * minDistance; } // Move the target and cam to the new point transform.position = newPosition; camTarget.position = newPosition; } else { // Move the target to the end point, so cam can lerp smoothly back. This works only because the ray is constructed through the target and not actual cam camTarget.position = clipZoomRayPlayerToCam.getEnd(); } }