Moving a game object Left and Right

I’m trying to make a GameObject move left and right, so I have the up and down code sorted. But in the following code it seems to set the new position and not move the GameObject itself.

I’ve been trying to work out what it could be but came to nothing.

This is the Code (It is a test script and there’s no other scripts fired from it):

public Vector3 StartLoc = new Vector3(0.0f,0.0f,0.0f);
public float MovementSpeed = 0.01f;
//Locations Vars
public Vector3 CurrentLocation;
public Vector3 NewLocation;
//Movement Bools
private bool AlreadyMoving = false;
private bool MoveUp = false;
private bool MoveDown = false;
private bool MoveLeft = false;
private bool MoveRight = false;

public GameObject SelectorGO;

// Use this for initialization
void Start () {
	CurrentLocation = StartLoc;
}

// Update is called once per frame
void Update () {
	if(Input.GetKeyDown (KeyCode.W) == true && AlreadyMoving == false)
	{
		NewLocation.z = CurrentLocation.z + 1.0f;
		MoveUp = true;
		AlreadyMoving = true;

	}
	if(Input.GetKeyDown (KeyCode.S) == true && AlreadyMoving == false)
	{
		NewLocation.z = CurrentLocation.z - 1.0f;
		MoveDown = true;
		AlreadyMoving = true;
	}
	if(Input.GetKeyDown (KeyCode.A) == true && AlreadyMoving == false)
	{
		NewLocation.x = CurrentLocation.x - 1.0f;
		MoveLeft = true;
		AlreadyMoving = true;
	}
	if(Input.GetKeyDown (KeyCode.D) == true && AlreadyMoving == false)
	{
		NewLocation.x = CurrentLocation.x + 1.0f;
		MoveRight = true;
		AlreadyMoving = true;
	}
	if(AlreadyMoving == true)
	{
		MovementCoding ();
	}
}

void MovementCoding(){
	if(MoveUp == true){
		if(CurrentLocation.z >= NewLocation.z){
			MoveUp = false;
			AlreadyMoving = false;
			CurrentLocation.z = NewLocation.z;

		}
		if(CurrentLocation.z < NewLocation.z){
			CurrentLocation.z = CurrentLocation.z + MovementSpeed;
			gameObject.transform.position = CurrentLocation;
		}
	}
	if(MoveDown == true){
		if(CurrentLocation.z <= NewLocation.z){
			MoveDown = false;
			AlreadyMoving = false;
			CurrentLocation.z = NewLocation.z;
			
		}
		if(CurrentLocation.z > NewLocation.z){
			CurrentLocation.z = CurrentLocation.z - MovementSpeed;
			gameObject.transform.position = CurrentLocation;
		}
	}
	if(MoveLeft == true){
		if(CurrentLocation.x >= NewLocation.x){
			MoveLeft = false;
			AlreadyMoving = false;
			CurrentLocation.x = NewLocation.x;
			
		}
		if(CurrentLocation.x < NewLocation.x){
			CurrentLocation.x = CurrentLocation.x - MovementSpeed;
			gameObject.transform.position = CurrentLocation;
			Debug.Log(CurrentLocation.x)
		}
	}
	if(MoveRight == true){
		if(NewLocation.x <= CurrentLocation.x){
			MoveRight = false;
			AlreadyMoving = false;
			CurrentLocation.x = NewLocation.x;
			
		}
		if(CurrentLocation.x > NewLocation.x){
			CurrentLocation.x = CurrentLocation.x + MovementSpeed;
			gameObject.transform.position = CurrentLocation;
			Debug.Log(CurrentLocation.x)
		}
	}
}

Thanks very much.

Dude, that’s the most complicated way to move an object that I’ve ever seen! There are too many boolean flags and ifs in this code to work - but enough to drive you nuts! Despite this, this script apparently should move the object to which it’s attached when the WASD keys are pressed. Is the script attached to the right object?

Anyway, I suggest you to change the code to something way simpler like this:

public float speed = 1; // speed in meters per second

void Update(){
  Vector3 moveDir = Vector3.zero;
  moveDir.x = Input.GetAxis("Horizontal"); // get result of AD keys in X
  moveDir.z = Input.GetAxis("Vertical"); // get result of WS keys in Z
  // move this object at frame rate independent speed:
  transform.position += moveDir * speed * Time.deltaTime;
}