Checking camera collision

I have this script that rotates the camera around the player using transform.rotatearound and transform.lookat.

playercamera.RotateAround(centerPoint.position, playercamera.up, turnerX);
playercamera.RotateAround(centerPoint.position, playercamera.right, -turnerY);
playercamera.LookAt(centerPoint);
CheckCameraCollision(playercamera);

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()

Whats supposed to happen:

What happens when it glitches:


Camera stuff is pretty tricky… you may wish to consider using Cinemachine from the Unity Package Manager.

There’s even a dedicated forum: Unity Engine - Unity Discussions

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.

private Vector3 WhereMyCameraIsLocated;
private Vector3 WhatMyCameraIsLookingAt;

void LateUpdate()
{
  cam.transform.position = WhereMyCameraIsLocated;
  cam.transform.LookAt( WhatMyCameraIsLookingAt);
}

Then you just need to update the above two points based on your GameObjects, no need to fiddle with rotations.

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

1 Like

You may not need to use LookAt when using transform.RotateAround as it sets both the position and the rotation of the transform.

The script below assumes the camera is a child of the player object.

using UnityEngine;

public class CameraOrbit : MonoBehaviour
{
    float idealDistance;
   
    void Start()
    {
        idealDistance=Vector3.Distance(transform.parent.position,transform.position);
    }

    void Update()
    {
        Vector3 pos=transform.parent.position+transform.parent.up;
        transform.RotateAround(pos,Vector3.up,Input.GetAxis("Mouse X"));
        transform.RotateAround(pos,transform.right,Input.GetAxis("Mouse Y"));
        if (Physics.Raycast(pos,transform.position-pos,out RaycastHit hit,idealDistance))
            transform.position=hit.point;
        else
            transform.position=Vector3.MoveTowards(transform.position,pos+(transform.position-pos).normalized*idealDistance,20*Time.deltaTime);
    }
}