I’m having trouble with the camera collisions and Vector3.SmoothDamp…
Here’s a video of the problem: Imgur: The magic of the Internet
If you know there’s a way to fix this, please let me know. I’ve been trying to fix it for a few hours but made no progress so far…
The code for my camera collider:
public GameObject behindCamera;
public GameObject character;
public GameObject cameraCenter;
public Camera cam;
public Transform playerTransform;
[Header("Collision Settings")]
public float collisionSensitivity = 1;
public float collisionDamp = 1;
public Vector3 currentVelocity = Vector3.zero;
public void HandleCameraCollisions()
{
behindCamera.transform.localPosition = new Vector3(cam.transform.localPosition.x, cam.transform.localPosition.y, cam.transform.localPosition.z - collisionSensitivity);
if(Physics.Linecast(cameraCenter.transform.position,behindCamera.transform.position, out _camHit)) //Check if the raycast from the player to the camera hits anything
{
//Move the camera to the raycast hit location
cam.transform.position = Vector3.SmoothDamp(cam.transform.position,_camHit.point,ref currentVelocity,collisionDamp*Time.deltaTime); //FIX THE SMOOTH DAMP
Vector3 localPosition = new Vector3(cam.transform.localPosition.x,cam.transform.localPosition.y,cam.transform.localPosition.z + collisionSensitivity); //So it won't be directly in the object it collides with
//but just a little in front
cam.transform.localPosition = localPosition;
}
Debug.DrawRay(cameraCenter.transform.position, behindCamera.transform.position, Color.green);
//Make sure the camera won't clip through the player
if(cam.transform.localPosition.z > distCameraCollisionFromPlayer)
{
cam.transform.localPosition = new Vector3(cam.transform.localPosition.x, cam.transform.localPosition.y, distCameraCollisionFromPlayer);
}
}