Something wrong with my script, << see video >>

see this - My Screencast

why the soldier is being displaced from the left,right positions, what is wrong going on ?

Sometimes soldier displaces to left, sometimes to right, sometimes both

I have noticed, its x values are not same for left and right positions, which i want them to be freezed. I dont know why they are changing with every click like for example if x = 3.1 [right side], then after going to left, when soldier is back to right then his position is x=2.99, where as it should be same everytime to x=3.1

whats wrong in the code

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){
			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);
			}
			if(LeftOrRight == false){
				//Debug.Log("entered");
				controller.Move(directionSelector*transform.right * (-SwitchingSpeed) * Time.deltaTime);
				
			}
			
			//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
       
}

My best guess without going through it all is that there is the possibility that you are getting two coroutines started. Have you tried setting isSwitching in the Update method instead of in the SwitchLanes function?

if (Input.GetMouseButton(1) !IsSwitching)
   {
   IsSwitching = true;
   ALTERNATOR = !ALTERNATOR;
   LeftOrRight = ALTERNATOR;
   Debug.Log(LeftOrRight);
   StartCoroutine(SwitchLanes());
   }

Then you would need to remove this:

        if (IsSwitching)
            yield return null;
        IsSwitching = true;

From the SwitchLanes function.

I have done as you guide, This has made my performance better but not 100% accurate. The Soldier is still not moving at FIXED “LEFT AND RIGHT” Positions. What else is wrong ?

Here is the video of how it is performing - http://screencast-o-matic.com/watch/cIfh66VrjI

And this is the latest code

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);
			}
			if(LeftOrRight == false){
				//Debug.Log("entered");
				controller.Move(directionSelector*transform.right * (-SwitchingSpeed) * Time.deltaTime);
				
			}
			
			//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
       
}

As far as I know, there are always some kind of differences in Unity between the ints of the transformation. Did you try to use Math.Clamp? I don’t use CharacterController, so I can’t help you directly, but I’m making some kind of similiar game with a slider to move my character, and i used:

transform.position.x = Mathf.Clamp(transform.position.x, -animateClamp, animateClamp); so make sure, he will never go out if this distance on the x-Axis

i am confused as how to change the script according to what you are saying, which line to change and to what. Can you help me more on that, my code is pasted above

So how can i change this line-

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

to

transform.position.x = Mathf.Clamp(transform.position.x…

I haven’t read all the coments nor the code but what about something like this:

var dir = directionSelector*transform.right * SwitchingSpeed * Time.deltaTime;
dir.x = Mathf.Clamp(dir.x, ...);
controller.Move(dir);

Trigve