Lerping Angles

I’m working on a very basic guard ai. As basic as it seems, I’m having trouble getting it to turn. At the moment, it will turn a few times if the turn angle is positive, but if the angle is negative, things start going wonky. If someone could take a look at my code and see if anything stands out as being wrong in some way, that’d be lovely. Thanks!

void FixedUpdate () 
	{
		distance = (transform.position - target.position).magnitude;
		if(contact == false && alertness > initalert && isRunning == false)
		{
			StartCoroutine (DecAlert());
		}
		if(distance < 300f)
		{
			Vector3 angle = (transform.position - target.position).normalized;
			float rangle = Vector3.Angle(transform.forward, angle);
			Debug.DrawRay(transform.position, -transform.forward, Color.blue, .5f);
			if(rangle < 85 && rangle > 45) //periferal vision
			{
				//raycast, if uninterupted, check movement, if moving, turn to get player in normal vision
				RaycastHit hit;
				if (Physics.Raycast(transform.position, -angle, out hit, 30f))
				{
					if(hit.transform.gameObject == target.gameObject){
						if(target.GetComponent<CharacterController>().GetMovement() != Vector3.zero)
						{
							Debug.DrawRay(transform.position, -angle, Color.red, 1f);
							tarpos = target.position;
							if(transform.eulerAngles.y - rangle < 0)
								tarrot = rangle - transform.eulerAngles.y;
							else
								tarrot = rangle + transform.eulerAngles.y;
							if(isTurning == false)
								StartCoroutine (TurntoTarget());
							Debug.Log("in perifery");
						}
					}
				}
			}
			else if (rangle <= 45) //normal vision
			{
				//raycast, if uninterupted, player is seen
				RaycastHit hit;
				if (Physics.Raycast(transform.position, -angle, out hit, 10f))
				{
					if(hit.transform.gameObject == target.gameObject){
						Debug.DrawRay(transform.position, -angle, Color.green, 1f);
						contact = true;
						tarpos = target.position;
						Debug.Log("in vision");
					}
				}
			}
		}
	}

IEnumerator TurntoTarget()
	{
		isTurning = true;
		float timer = Time.time;
		Vector3 origin = transform.rotation.eulerAngles;
		float distan;
		if(tarrot > 180f)
			tarrot -= 360f; //this is going to the rotation that is equal to the angle, not the actual angle
		Debug.Log(tarrot);
		while (transform.eulerAngles.y != origin.y + tarrot){
			distan = ((Time.time - timer) * speed * speed * 3) / Mathf.Abs(origin.y - tarrot);
			transform.eulerAngles = new Vector3(transform.rotation.x, Mathf.LerpAngle(origin.y, origin.y + tarrot, distan), transform.rotation.z);
			yield return null;
			Debug.Log("Rotation = " + transform.eulerAngles + " Angle = " + tarrot + " Distan = " + distan);
		}
		isTurning = false;
		Debug.Log("end");
	}

Target is set to a player game object

If your problem is that the object is rotating the same direction no matter if turning the opposite way is shorter, this is caused by using eulerangles. If you want to know the difference between eulerangles and quaternions there are lots of reference/explanations around. Try using Quaternions, transform.rotation and Quaternion.Lerp to see if your issue is solved.

An alternative is:

transform.LookAt(target);
transform.eulerAngles = new Vector3(0 ,transform.eulerAngles.y, 0);