private Vector3 CalculateCollisionAdjustments() { Vector3 adjustment = Vector3.zero; CollisionHandler.UpdateCameraClipPoints(transform.position, transform.rotation, ref CollisionHandler.AdjustedCameraClipPoints); CollisionHandler.UpdateCameraClipPoints(m_finalPosition, transform.rotation, ref CollisionHandler.DesiredCameraClipPoints); CollisionHandler.CheckColliding(FollowObject.transform.position); if (CollisionHandler.Colliding) { float adjustmentDistance = CollisionHandler.GetAdjustedDistanceWithRayFrom(FollowObject.transform.position); adjustment = Quaternion.Euler(transform.eulerAngles.x, transform.eulerAngles.y + FollowObject.eulerAngles.y, 0) * -Vector3.forward * (adjustmentDistance - Vector3.Distance(FollowObject.transform.position, m_finalPosition)); } adjustment = Vector3.SmoothDamp(m_lastAdjustment, adjustment, ref m_cameraLerpVelocity, AdjustSmoothing); return(adjustment); }
void CamControl() { mouseXValue += Input.GetAxis(mouseXId) * rotationSpeed; mouseYValue -= Input.GetAxis(mouseYId) * rotationSpeed; mouseYValue = Mathf.Clamp(mouseYValue, -35, 60); //allow player to control camera seperate from player movement if (Input.GetKey(cameraFreeMovementKey)) { target.rotation = Quaternion.Euler(mouseYValue, mouseXValue, 0); } //turn player along with camera's rotation else { target.rotation = Quaternion.Euler(mouseYValue, mouseXValue, 0); player.rotation = Quaternion.Euler(0, mouseXValue, 0); } //zoom controls and functionality if (Input.GetAxis(mouseScrollId) != 0) { //prevent getting stuck in a loop when zooming back out from 0 distance; if (distanceFromTarget == 0) { distanceFromTarget++; } distanceFromTarget += -Input.GetAxis(mouseScrollId) * zoomSpeed; distanceFromTarget = Mathf.Clamp(distanceFromTarget, minZoomDistance, maxZoomDistance); } //enter first person view when zooming in at a close distance else if (distanceFromTarget < 2) { distanceFromTarget = 0; } //using raycasts here collision.CheckColliding(target.position); adjustmentDistance = collision.GetAdjustedDistanceWithRayFrom(target.position); if (collision.colliding) { //enter first person view when colliding at a close distance behind player if (adjustmentDistance < 1) { adjustmentDistance = 0; } adjustedDestination = target.position - transform.forward * adjustmentDistance; if (smoothFollow) { //smoothening camera's position change transition transform.position = Vector3.SmoothDamp(transform.position, adjustedDestination, ref camVel, smooth); } else { transform.position = adjustedDestination; } } else { destination = target.position - transform.forward * distanceFromTarget; if (smoothFollow) { transform.position = Vector3.SmoothDamp(transform.position, destination, ref camVel, smooth); } else { transform.position = destination; } } }