Mathf.Lerp between values for blending.

Hey guys,
so I’m trying to make a smooth transition between my running and walking animation. Here’s what I’ve got.

if (Input.GetButton ("Fire1")) {

				anim.SetBool ("moving", true);
				float blendTime = 0;
				blendTime = Mathf.Lerp (0f, 1f, Time.time);
				anim.SetFloat ("Blend", blendTime);
				transform.Translate (Input.GetAxis ("Horizontal") * walkSpeed * sprintMultiplier * Time.deltaTime, 0f, Input.GetAxis ("Vertical") * walkSpeed * sprintMultiplier * Time.deltaTime, Space.World);

			} else {

				anim.SetBool ("moving", true);
				float blendTime = 1;
				blendTime = Mathf.Lerp (1f, 0f, Time.time);
				anim.SetFloat ("Blend", blendTime);
				transform.Translate (Input.GetAxis ("Horizontal") * walkSpeed * Time.deltaTime, 0f, Input.GetAxis ("Vertical") * walkSpeed * Time.deltaTime, Space.World);

			}

Somehow it doesn’t work and snaps between changing animations. Why?

The value of Time.time represents the amount of seconds since game start. So after one second, it will never be below 1.0 again.

Now, Mathf.Lerp’s third parameter is being clamped between 0 and 1. This means that after second one, this statement

Mathf.Lerp (0f, 1f, Time.time);

will constantly be one. Remember that Lerp functions do not animate anything they just take three values and return a value with the formula lerp(a, b, t) = a + (b - a) * t.

But rather than solving this problem on the scripting level, why not just use the blending features of mecanim? Blending between animations is one of mecanim’s core functions, and you only need to set it up in the mecanim window with no additional scripting required.