Why is .Lerp so bumpy?

I am trying to make my own gun script and for some reason my gun is acting super bumpy. My script is:

    public Transform gunHolder;
	public float holdSide = 0.3f;
	public float holdHeight = -0.2f;
	public float moveSpeed = 1;
	public float rotateSpeed = 1;
	
	
	void Start ()
	{
		
	}
	
	void Update ()
	{
		gunHolder.localPosition = new Vector3(holdSide, holdHeight, 0);
		
		transform.position = Vector3.Lerp(transform.position, gunHolder.position, moveSpeed);
		transform.rotation = Quaternion.Lerp(transform.rotation, gunHolder.rotation, moveSpeed);
	}

Does anyone know a better way of doing this? Thanks.

It could be partly due to different frame durations. You can test that by adding a random-length delay to your Update function and observing how well your game deals with it.

To compensate for that, for the last parameter to either Lerp function, pass:

1 - Mathf.Exp(-k * Time.deltaTime)

Here ‘k’ is a constant that controls the lerp rate.

Some people use a multiple of Time.deltaTime as the third parameter here, but that is wrong for Lerp - you should use an exponential of the form I stated above, to properly correct for varying frame times.

Beyond that, if the target object is moving with a fairly constant speed then you’ll still get some jitter, and can do even better with an alternate lerp function. See this thread for more details:

http://forum.unity3d.com/threads/130920-How-to-smooth-damp-towards-a-moving-target-without-causing-jitter-in-the-movement

and the webplayer demo I uploaded here (the bottom cube uses my custom lerp function):

http://www.gfootweb.webspace.virginmedia.com/SmoothDamp/WebPlayer.html

moveSpeed = 1 means you jump in one frame without smoothing.
Try 0.1f for starters and play around with it till it feels right

The third parameter should be a number between 0 and 1.

If you imaging a line going from the first parameter to the second parameter, the third parameter represents percentage of progress along than line that is used for the return value.

Here is a table that may help visualise the concept.

first | second | third | return
-------------------------------
 10   |   20   |  0.00 |   10
 10   |   20   |  0.25 |   12.5
 10   |   20   |  0.50 |   15
 10   |   20   |  0.75 |   17.5
 10   |   20   |  1.00 |   20

For a lerp to look smooth and even it should a stable start and end values with a “t” value that ranges between 0 and 1.

To take your postion lerp as an example:

  • The start value (transform.position) is changing as it is updated every time.
  • The end value (gunHolder.position) may be changing (player input?).
  • The “t” value is fixed.

This would mean that you would be move a fixed percentage closer to the end value on each frame.

This is an example with a “t” of 0.5 (to make the maths easier I am treating start & end as stable) :

frame# | first | second | third | return   | delta
--------------------------------------------------
   1   |  10   |   20   |  0.5  |   15     |  5
   2   |  10   |   20   |  0.5  |   17.5   |  2.5
   3   |  10   |   20   |  0.5  |   18.75  |  1.25
   4   |  10   |   20   |  0.5  |   19.375 |  0.625

As you can see the rate of movement (delta) starts high but quickly tends towards zero.
For this use of lerp the trick to to find a “t” value that is not too high and not too low, it involves manual tweaking and a judjment call.