I ran into the problem that the camera clips through walls so I added a function to deal with to try to avoid that
Vector3 desiredPosition = cam.position;
RaycastHit hit;
if(Physics.Linecast(player.transform.position, desiredPosition, out hit, ~layertoignore))
{
//Camera distance minmax.x is lowest and minmax.y is max
cameraZoom = Mathf.Clamp(hit.distance, cameraDistanceMinMax.x, cameraDistanceMinMax.y);
}
else
{
cameraZoom = cameraDistanceMinMax.y;
}
Vector3 zoomVector = -playercamera.forward * zoom;
playercamera.position = centerPoint.position + zoomVector;
In unity my player and camera are seperate objects. When I use this script sometimes it will do what its supposed to do and zoom in closer to the player but sometimes the camera doesn’t zoom in more, is there a fix for this? Also I’m running the camera script in LateUpdate()
If you insist on making your own camera controller, the simplest way to do it is to think in terms of two Vector3 points in space: where the camera is LOCATED and where the camera is LOOKING.
Found that the problem was that it doesn’t check for the collision if there is no mouse input, i just made it always check in case the player is moving but not the camera