I have a third person camera that is targeted to my main character, I want to shift the camera left and right when I move my firing joystick accordingly, currently I am doing this by checking if my firing joystick is active, and if it is, I add my firing joystick x-position to my camera transform x-position like so:
cameraTransform.position.x += firingJoystick.position.x;
This works fine except for the fact that the camera snaps into position, and snaps back again when I release the joystick. I thought it would be an easy affair to smooth this out using SmoothDamp or Lerp, but I can't seem to figure out how to do it. As soon as I start experimenting with those things the camera shift script starts (I suspect) interfering with the rest of my camera script and the camera starts shaking, trying to be in two places at once. Does anybody mind giving me some pointers on how I can smooth out this camera shift while still just adding to the current camera position so as to avoid interfering with the rest of the script?
I'd been trying to implement a smoothing script with Lerp, and came across the same problems you did. Turns out that the script in the docs is rather misleading. Here's why: Lerp sets a value between min and max (in this case, your camera position), and sets it to a value that reflects a decimal number between 0 and 1 that you put in. For example, say you called Lerp like this: `Mathf.Lerp(0, 5, Time.time), 0, .5);` You'd get 2.5 back as a result, which is .5 (50%) between 0 and 5.
Now, the problem with the documentation's example is that they're using Time.time as the interpolation value, which means that your Lerp will only work for the first second of the game, as that's the only time when Time.time is between 0 and 1, resulting in a usable decimal number for the Lerp function. (At least, that's what I recall being the problem for me)
Now, after that lengthy wall of text, here's your solution: forget Lerp. Instead, use `Input.GetAxis`, which returns values that are already smoothed and prettied up for you to use in your controller. You'd call it like this:
cameraTransform.position.x += Input.GetAxisHorizontal * movementSpeed;
Where `movementSpeed` is the speed you want to move at.
Instead of updating the camera position immediately, you could add a local variable "Vector3 TargetPosition", and update that instead. Then lerp the camera position towards the target position each frame, something like ...
cameraTransform.position = Vector3.Lerp(cameraTransform.position, TargetPosition, 5.0f * Time.deltaTime);
Or for just the x component ...
cameraTransform.position.x = Mathf.Lerp(cameraTransform.position.x, TargetPosition.x, 5.0f * Time.deltaTime);
Note that 5 is a damping parameter -- make this larger to snap more quickly to the target position, smaller to move more slowly.
Also note that updating the camera position is usually best done in the LateUpdate method rather than Update, in your case to get the latest changes in firingJoystick, otherwise you may have the previous frame's data, which could cause jitter (though with the damping you probably wouldn't notice).