MAthf.clamp and controller.Move problem, Really need a solution

This is my code, Dont worry read it and help me, it wont take more than 2 minutes :slight_smile:

using UnityEngine;
using System.Collections;
 
public class characterScript : MonoBehaviour
{
        //references
	public float SwitchingSpeed;
	public float ForwardSpeed;
	public int HowFarToGo;
	private bool OnLeft;
	private bool OnRight;
	private bool IsSwitching;
	private int counter;
	private bool ALTERNATOR, LeftOrRight;
	
	
        private CharacterController controller;
        private string activeAnimation;
	
       
        // move variables
        public float runSpeed = 8.0f;
       public static float hero_z_position;
	
	
	void Start ()
	{
		ALTERNATOR = true;
		OnRight = true;
		controller = gameObject.GetComponent<CharacterController>();
	}
       
	void Update ()
	{
		hero_z_position = transform.position.z;
		controller.Move(transform.forward * 8f * Time.deltaTime);
		animation.CrossFade("Run",0.0f);
		if (Input.GetMouseButton(1) !IsSwitching){
			IsSwitching = true;
			ALTERNATOR = !ALTERNATOR;
                LeftOrRight = ALTERNATOR;
			Debug.Log(LeftOrRight);
			StartCoroutine(SwitchLanes());}

	}
	
	private IEnumerator SwitchLanes ()
	{
		//if (IsSwitching)
		//	yield return null;
		//IsSwitching = true;
		
		while (counter < HowFarToGo)
		{
			counter++;
			int directionSelector = OnRight ? 1 : -1;
			// why my soldier is not moving left,right ??
			
			
			if(LeftOrRight == true){
				//Debug.Log("going left.."+LeftOrRight);
				//controller.Move(directionSelector*transform.right * SwitchingSpeed * Time.deltaTime);
				Vector3 pos = transform.position;
				pos.x = Mathf.Clamp(pos.x,2.0f,2.0f);
				transform.position = pos;
				
			}
			if(LeftOrRight == false){
				//Debug.Log("entered");
				//controller.Move(directionSelector*transform.right * (-SwitchingSpeed) * Time.deltaTime);
				Vector3 pos = transform.position;
				pos.x = Mathf.Clamp(pos.x,-2.0f,-2.0f);
				transform.position = pos;
			
			}
			
			//controller.Move(directionSelector*transform.right * SwitchingSpeed * Time.deltaTime);
			//animation.CrossFade("RunJump", 0.0f);
			yield return new WaitForEndOfFrame();
		}
		counter = 0;
		IsSwitching = false;
	}

 
         //Update is called once per frame
       
}

In this code i used this code of line to move my player from left to right and right to left [see line 62 and 70 above]
Problem:: The x position does not remain same, it changes like x=3.1 will change to x=2.9 , I dont know how to tackle with this solution

//controller.Move(directionSelector*transform.right * SwitchingSpeed * Time.deltaTime);

Then i tried mathf.clamp**[see line 60 above]**, i am using this line of code but what is happening is my soldier is standing at x=2.99, first it goes to 1.99, then on next click to 5.99 and good news is these positions are always same. Why he goes to 1.99, i want him to be at 2.99 only and its movement is instant, cant it be diognal,as my controller.Move… so that it look more realistic ??

	if(LeftOrRight == true){
				//Debug.Log("going left.."+LeftOrRight);
				//controller.Move(directionSelector*transform.right * SwitchingSpeed * Time.deltaTime);
				Vector3 pos = transform.position;
				pos.x = Mathf.Clamp(pos.x,2.0f,2.0f);
				transform.position = pos;
				
			}
			if(LeftOrRight == false){
				//Debug.Log("entered");
				//controller.Move(directionSelector*transform.right * (-SwitchingSpeed) * Time.deltaTime);
				Vector3 pos = transform.position;
				pos.x = Mathf.Clamp(pos.x,-2.0f,-2.0f);
				transform.position = pos;
			
			}

Ok just a carefull guess:
In the LeftOrRight == false clause
you use ‘-SwitchingSpeed’ although directionSelector is already negative so the result is positive again just like in LeftOrRIght == true clause.

Also line 77, the extra Move() doenst make sense to me.

line 77 is commented

and checked “-” has to be there