Need help space invader enemy movent not working!

I wrote a script for a game similar to a space invaders remake, and I am working on the movement controls of the enemy. They should move to the right, once they get to edge of screen, drop down a line and start moving to the left, drop down a line etc. it works fine to the right, but with the exact same code to the left it doesnt? what is the problem?

using UnityEngine;
using System.Collections;

public class EnemyMovement : MonoBehaviour {
	
	//spawn random range for position / ammount to spawn.
	public float leftRightSpeed;
	
	public bool WeShouldGoLeft;
	public bool WeShouldGoRight;
	
	public float dropAmmount = 1f;
	
	public Transform myTransform;
	
	public float boundryLeft = 6.6f;
	public float boundryRight = 6.8f;
	
	
	void Awake()
	{
		WeShouldGoRight = true;
	}

	// Update is called once per frame
	void Update () 
	{
		
		// -6.8 left  +6.5 right.
		if(transform.position.x >= 6.2f)
		{
			WeShouldGoLeft = true;
			WeShouldGoRight = false;
			DropALevel();
			print ("MovingRight");
		}
		
		
		
		// -6.8 left  +6.5 right.
		if(transform.position.x <= -5)
		{
			WeShouldGoRight = true;
			WeShouldGoLeft = false;
			DropALevel();
			print ("MovingLeft");
		}
		
		if(WeShouldGoLeft)
		{
			MoveLeft();
		}
		if(WeShouldGoRight)
		{
			MoveRight();
		}
		
	}
	
	void MoveRight()
	{
		//move left constant.
		transform.Translate(new Vector3(leftRightSpeed * Time.deltaTime,0,0));
	}
	
	void MoveLeft()
	{
		//move left constant.
		transform.Translate(new Vector3( - leftRightSpeed * Time.deltaTime,0,0));
	}
	
	void DropALevel()
	{
		Vector3 position = transform.position;
		position.y -= dropAmmount;
		transform.position = position;
	}
}

I don’t see anything technically wrong with what you’ve posted. What I do see is a lot of copied code. A lot of times people feel like they just want to hack something in there, but this can lead to problems down the road as well as immediate bugs (which is what I suspect is happening here).

Here’s one example of what I mean by copied code. You took this line:

transform.Translate(new Vector3(leftRightSpeed * Time.deltaTime,0,0));

and copied and pasted it below (or at least you should have, for efficiency), then changed one part. Any time you copy code like this, a little warning flag should go off in your head. It should say, “Should I really be copying code like this?” The answer is almost always “No”. The reason is that every single line of code represents a potential for bugs and errors. As such, when you have an easy opportunity to reduce lines of code without adding (much) complexity, you should always do so.

Another, perhaps less obvious example of duplicated code, is your two boolean variables. Since these variables are ALWAYS opposite (e.g. you’ll never have WeShouldGoLeft and WeShouldGoRight both true), you can get rid of one of them.

Sorry if this seems like a bit of a lecture. The reason why I’m talking about this is that your stated bug, which is that left movement behaves differently from right movement, would be a lot less likely to occur if left and right followed a nearly-identical code path.

If you want to puzzle through the refactor yourself, go ahead and take the time to do so. I’ll identify the parts of your code that are copied; see if you can combine them together yourself. See how many lines you can cut out. I’ll post my version of the code a little later, and you can compare the two.

SPOILER ALERT

Here’s the updated code. I encourage you to try the refactor on your own before looking at mine.

I kept your variable names (fixed random spelling errors) and comments, for the most part. How much code did I save? Without the “using” statements at the top, my code is 37 lines, which is a full 50% reduction from the original. Full disclosure: I did remove some white space (blank lines), but not that many. The majority of savings comes from code consolidation.

I used the C# “ternary operator” (?) a couple of times, pretty much just to make the code as small as possible. This is essentially an if/else statement in a single line, and can be useful for condensing code.

I also changed some of your variables to remove “public”. You shouldn’t use “public” unless you want these variables accessible by someone else (like the scene). This “encapsulation” will strengthen your code by removing one source of errors: outside tampering.

Without further ado, here it is. Ok, one more ado. I didn’t test this code, so I wouldn’t be shocked if you find a bug or two. Compare it to your refactor, and see if it solves your initial problem. Even if it doesn’t, though, it might help you figure out the solution.

using UnityEngine;
using System.Collections;
 
public class EnemyMovement : MonoBehaviour
{
	//spawn random range for position / amount to spawn.
	public float leftRightSpeed;
	bool WeShouldGoRight = true;
	Vector3 dropAmount = new Vector3(0, 1, 0);
	public Transform myTransform;
	float boundaryLeft = -6.6f;
	float boundaryRight = 6.8f;
 
	// Update is called once per frame
	void Update ()
	{
		// 6.2 is a "magic number"; where did it come from?  I'm going to assume that you want 
		// to use your boundary variables instead.
		if ((transform.position.x >= boundaryRight) || (transform.position.x <= boundaryLeft)) 
		{
			// A boundary has been reached; swap the travel direction and move down a level.
			WeShouldGoRight = !WeShouldGoRight;
			DropALevel();
			print ("Switching directions; now moving " + (WeShouldGoRight ? "Right" : "Left"));
		}
		
		Move(WeShouldGoRight);
	}
 
	void Move(bool moveRight)
	{
		float movementX = moveRight ? leftRightSpeed : -leftRightSpeed;
		transform.Translate (new Vector3 (movementX * Time.deltaTime, 0, 0));
	}
 
	void DropALevel()
	{
		transform.Translate(dropAmount);
	}
}