Hey guys! I’ve looked through a lot of the answers and managed to get some smooth camera code to follow my target. However I wanted to add bounds to the current level that the camera cannot see past, so I found the clamp function.
After adding the clamp function my camera worked correctly at around 60fps. Today I went to go test my game again and it was running at around 400fps (Not sure if this is relevant) but the camera was violently shaking and no changes to code have been performed.
Attached is my code and I am not sure what I need to change to fix this problem:
void followPlayer(){
float cameraClampX = Mathf.Clamp(transform.position.x, -15, 13);
float cameraClampY = Mathf.Clamp(transform.position.y, 2, 14);
Vector3 point = camera.WorldToViewportPoint(target.position);
Vector3 delta = target.position - camera.ViewportToWorldPoint(new Vector3(0.4f, 0.4f, point.z)); //(new Vector3(0.5, 0.5, point.z));
Vector3 destination = transform.position + delta;
Vector3 currentplace = new Vector3(cameraClampX, cameraClampY, transform.position.z);
//Vector3 cameraMove = Vector3.MoveTowards(currentplace, destination, 0.5f);
Vector3 cameraMove = Vector3.SmoothDamp(currentplace, destination, ref velocity, dampTime);
transform.position = cameraMove;
}
If anyone would like to explain to me a more efficient way of making this work or something that will even fix this problem I would be extremely grateful!
Thank you