3rd Person Camera flies off "to infinity...and beyond!"

So some time ago I was working on a 3rd person controller and I’ve recently decided to start again from scratch.
I’ve positioned a camera target slightly above the player’s head and the cam is currently looking at it using the LookAt function that I’m intending to replace by some self-made code later on. I simply move it around the player using the transform.Translate function.

transform.LookAt(camTarget);

float horizontal = Input.GetAxis("Mouse X") * moveSpeed * Time.deltaTime;
float vertical = Input.GetAxis("Mouse Y") * moveSpeed * Time.deltaTime;
transform.Translate(horizontal, -vertical, 0f);

So that way, it works out pretty well except that the distance between the camera and the player keeps increasing when moving the cam around, possibly because the LookAt function can’t keep up with the cam’s translation.

So I tried to fix it by subtracting the desired offset from the actual cam’s distance to the player and add that value to the cam’s local z-axis in order to move it back towards the player. And it works, if you move the cam really slowly. The distance always gets adjusted to my offset variable.

camDistance = Distance(transform.position, camTarget.position); // I know there's a Unity distance function but I've made that one myself
float zFix;

if(camDistance > camOffset){
	zFix = transform.localPosition.z + (camDistance - camOffset);
	transform.localPosition = new Vector3(transform.localPosition.x, transform.localPosition.y, zFix); 
}

However, if you move the cam a bit faster, the Debug.Log distance output increases incredibly fast, ending up displaying “Infinity”.
Pausing the scene doesn’t let you focus on the camera anymore, which I guess means that Unity completely lost track of where it is.
It just flies off into infinity.

I haven’t got any idea so far why this is happening since the code is rather simple.
Soo… hope some of you can help me out. :slight_smile:

Best regards.

PS: All the code above is in LateUpdate().

For the camera movement controls something like “transform.RotateAround(target.position,rotationV+rotationH)” would be better suited I think.
About the following of the target: Could you make the camera child of the target? If not
I would translate the camera every frame just like the character was translated.

I’m using the LookAt function only for now, I’m going to replace it with some custom code later on anyway.
I’ve had the camera parented in the target before and did the rotation only by rotating the target itself, but I wanted to get
rid of that to increase the usability.

Still, I really wonder why the camera suddenly disappears in an infinite distance. That’s what bugs me the most right now.

Does its move away slower when you descrease the movespeed?

No, it doesn’t seem to have anything to do with the move speed. And if it does, you wouldn’t see any changes since the speed increase is very rapid, reaching infinity in less than a second.

What happens when you comment out the Transform.Translate? Does the problem go away? If so you should remove it and just rotate the camera around the target rather than translating it.

Well, the problem goes away because that is the only function that actually moves the camera.
But I’ll replace the LookAt and Translate function with some custom code now. Should work, but it’s kinda sad if I never find out what the actual problem was.