Reduce float speed on keypress

So I have this code:

using UnityEngine;
using System.Collections;
using UnityEngine.AI;

public class WanderingAi : MonoBehaviour {

	public float wanderRadius;
	public float wanderTimer;

	public Transform target;
	private NavMeshAgent agent;
	public float timer;
	public Animator playerAnimator;
	float someMovementSpeedThatMakesSense = 0.1f;
	public float _baseSpeed = 6f;
	public HealthOverall damageTrigger;
	public WanderingAi wanderingAiScript;
	float walkSpeed = 2.5f;
	float runSpeed = 6f;
	float idleSpeed = 0f;
	float changeSpeedTime = 0.5f;
	public bool running;

	// Use this for initialization
	void OnEnable () {
		agent = GetComponent<NavMeshAgent> ();
		timer = wanderTimer;
	}

	void Lerping()
	{

	}

	// Update is called once per frame
	void FixedUpdate () {
		timer += Time.deltaTime;
		float velocity = agent.velocity.magnitude;
		var speed = Vector3.Distance(agent.desiredVelocity, new Vector3(0, 0, 0));

		playerAnimator.SetFloat("Speed", speed);
		playerAnimator.speed = speed <= 0 ? 1 : speed / _baseSpeed;
		if (damageTrigger.isDead == true) {
			agent.enabled = false;
			wanderingAiScript.enabled = false;
		}
		//			agent.updatePosition = false;
//		agent.updateRotation = true;
		if (timer >= wanderTimer) {
//			agent.speed = Mathf.Lerp (runSpeed, idleSpeed, changeSpeedTimeDown);
			StartCoroutine (WaitforNextMove());
			Vector3 newPos = RandomNavSphere (transform.position, wanderRadius, -1);
			agent.SetDestination (newPos);
			timer = 0;
			agent.Resume ();

		}
		if (Input.GetKeyDown("space")){
//			agent.speed = Mathf.Lerp (runSpeed, idleSpeed, changeSpeedTimeDown);
			StartCoroutine (WaitforNextMove());
			Vector3 newPos = RandomNavSphere (transform.position, wanderRadius, -1);
			agent.SetDestination (newPos);
			timer = 0;
			agent.Resume ();
			Debug.Log ("Changed Destination");
		}
		if (Input.GetKey(KeyCode.LeftShift)) {
			running = true;
			changeSpeedTime += Time.deltaTime;
			agent.speed = Mathf.Lerp (walkSpeed, runSpeed, changeSpeedTime);
		}

		if (Input.GetKeyUp(KeyCode.LeftShift)){
			running = false;
		}

		if (running == false) {
//			changeSpeedTime += Time.deltaTime;
			agent.speed = Mathf.Lerp (walkSpeed, runSpeed, Time.deltaTime);
		}
	}
		

	public static Vector3 RandomNavSphere(Vector3 origin, float dist, int layermask) {
		Vector3 randDirection = Random.insideUnitSphere * dist;

		randDirection += origin;

		NavMeshHit navHit;

		NavMesh.SamplePosition (randDirection, out navHit, dist, layermask);

		return navHit.position;
	}

	IEnumerator WaitforNextMove()
	{
		yield return new WaitForSeconds (0.5f);
	}
}

and upon releasing shift button, I want the lerp values in fixed update (where bool running is) to decrease back to walkspeed. Problem is, upon pressing shift, lerp completely stops working and the change is sudden instead of smooth. First time it works just fine when pressing shift, value indeed increases smootly, but then it stops working when going back. This all is for navmeshagent.

Any solutions?

When shift is down, you are lerping from walk to run like so:

agent.speed = Mathf.Lerp (walkSpeed, runSpeed, changeSpeedTime);

However, when shift is up, you’re still lerping from walk to run:

agent.speed = Mathf.Lerp (walkSpeed, runSpeed, Time.deltaTime);

What you probably want to do is lerp from run to walk on the second one:

agent.speed = Mathf.Lerp (runSpeed, walkSpeed, Time.deltaTime);

Sadly, it doesn’t seem to work, I swapped these values, but lerp works only once for some reason. Even manually reducing speed on navmeshagent from inspector to 2.5 and then pressing shift again - lerping doesn’t seem to work anymore.

@EDIT: Actually the answer was simplier than I thought. No lerp or any of that jazz… just simple ifs.

using UnityEngine;
using System.Collections;
using UnityEngine.AI;

public class WanderingAi : MonoBehaviour {

	public float wanderRadius;
	public float wanderTimer;

	public Transform target;
	private NavMeshAgent agent;
	public float timer;
	public Animator playerAnimator;
	float someMovementSpeedThatMakesSense = 0.1f;
	public float _baseSpeed = 6f;
	public HealthOverall damageTrigger;
	public WanderingAi wanderingAiScript;
	float walkSpeed = 2.5f;
	float runSpeed = 6f;
	float idleSpeed = 0f;
	float changeSpeedTime = 0.5f;
	public bool running;

	// Use this for initialization
	void OnEnable () {
		agent = GetComponent<NavMeshAgent> ();
		timer = wanderTimer;
	}

	void Update()
	{
		if (agent.speed > 6) {
			agent.speed = 6;
		}
	}

	// Update is called once per frame
	void FixedUpdate () {
		timer += Time.deltaTime;
//		changeSpeedTime += Time.deltaTime;
		float velocity = agent.velocity.magnitude;
		var speed = Vector3.Distance(agent.desiredVelocity, new Vector3(0, 0, 0));

		playerAnimator.SetFloat("Speed", speed);
		playerAnimator.speed = speed <= 0 ? 1 : speed / _baseSpeed;

		if (damageTrigger.isDead == true) {
			agent.enabled = false;
			wanderingAiScript.enabled = false;
		}
		//			agent.updatePosition = false;
//		agent.updateRotation = true;
		if (timer >= wanderTimer) {
//			agent.speed = Mathf.Lerp (runSpeed, idleSpeed, changeSpeedTimeDown);
			StartCoroutine (WaitforNextMove());
			Vector3 newPos = RandomNavSphere (transform.position, wanderRadius, -1);
			agent.SetDestination (newPos);
			timer = 0;
			agent.Resume ();

		}
		if (Input.GetKeyDown("space")){
//			agent.speed = Mathf.Lerp (runSpeed, idleSpeed, changeSpeedTimeDown);
			StartCoroutine (WaitforNextMove());
			Vector3 newPos = RandomNavSphere (transform.position, wanderRadius, -1);
			agent.SetDestination (newPos);
			timer = 0;
			agent.Resume ();
			Debug.Log ("Changed Destination");
		}

		if (Input.GetKey(KeyCode.LeftShift)) {
			running = true;
//			changeSpeedTime += Time.deltaTime;
			agent.speed += 1f * Time.deltaTime;
			if (agent.speed > 6) {
				agent.speed = 6;
			}
		}
		//
		if (Input.GetKeyUp(KeyCode.LeftShift)){
			running = false;
		}
		//
		else if (running == false) {
			agent.speed -= 1f * Time.deltaTime;
			if (agent.speed < 2.5f) {
				agent.speed = 2.5f;
			}
//			changeSpeedTime += Time.deltaTime;
//			agent.speed = Mathf.InverseLerp (walkSpeed, walkSpeed, changeSpeedTime);
		}
	}
		

	public static Vector3 RandomNavSphere(Vector3 origin, float dist, int layermask) {
		Vector3 randDirection = Random.insideUnitSphere * dist;

		randDirection += origin;

		NavMeshHit navHit;

		NavMesh.SamplePosition (randDirection, out navHit, dist, layermask);

		return navHit.position;
	}

	IEnumerator WaitforNextMove()
	{
		yield return new WaitForSeconds (0.5f);
	}
}

…yeah I know code is messy, I will have to clean it up later. It works though.