Enemy rotates around after finished moving, in wrong direction

Hello. I have a 2D enemy that sets its rotation based on which direction it moved in the previous frame. I did not have this problem yesterday, but it just started today before I started working?

Once the enemy reaches a certain distance from the player, it attacks, then waits. After waiting it will determine if it needs to move more or if it needs to attack again, based on distance.

My problem is that once the enemy attacks the player while facing right (or at 180 degrees) it immediately flips its rotation to left (0 degrees). However if the enemy is already facing left, then attacks, it stays at its proper angle.

Here is the code if anyone could please give some information to educate me.

public class EnemyMovement1 : MonoBehaviour {

	Animator anim;

	public GameObject target;
	public float inRangeDistance;
	public float speed = 0.5f;
	public bool attack;

	//Handles ammo stuff
	public GameObject smokeRing;
	public GameObject spawnObj;
	Vector2 spawnpoint;

	Vector3 lastPos;
	public float threshold = 0.0f;

	// Use this for initialization
	void Start () {

		anim = GetComponent<Animator>();
		target = GameObject.Find ("PlayerConservative");
		lastPos = transform.position;


	// Update is called once per frame
	void Update () {

		//animator stuff
		anim.SetBool ("attack", attack);
		anim.SetBool ("moving", false);
		anim.SetFloat ("vSpeed", rigidbody2D.velocity.y);

		//Direction Stuff
		Vector3 offset = transform.position - lastPos;
		if(offset.x > threshold){
			lastPos = transform.position;
			transform.rotation = Quaternion.AngleAxis(180, Vector3.up);
			anim.SetBool ("moving", true);
		else if (offset.x < threshold){
			lastPos = transform.position;
			transform.rotation = Quaternion.AngleAxis(0, Vector3.up);
			anim.SetBool ("moving", true);

		//In Range Stuff
		inRangeDistance = Vector2.Distance (transform.position, target.transform.position);
		if(inRangeDistance < 3 && inRangeDistance > 1 && !attack){
			float movementSpeed = speed * Time.deltaTime;
			transform.position = Vector2.MoveTowards(transform.position, new Vector2(target.transform.position.x, transform.position.y), movementSpeed);
 		else if(inRangeDistance > 3){
			transform.position = transform.position;
		else if(inRangeDistance < 1){
			transform.position = transform.position;

	IEnumerator Attack(){
		attack = true;
		spawnpoint = spawnObj.transform.position;
		GameObject clone;
		clone = Instantiate (smokeRing, spawnpoint, transform.rotation) as GameObject;
		yield return new WaitForSeconds(3);
		attack = false;


I did not have this problem yesterday, but it just started today before I started working?

we all had that one yesterday and not today. very common problem. i had a similar problem yesterday and today it is ok, so perhaps the error was transmitted from the sun rotation to unity rotation lol. (concise is cool with unity answers)

… you are using quaternion anglaxis in 2d!!! quaternion is for 3d stuff like rotating planes.

for 2d you can entirely get away with rotation.eulerAngle you also have transform.lookat, to look at away from previous position or player.

it’s a relatively simple task, but your code is not so simple for me. just rewrite the rotation line so that it looks at last position or player. you can do attacker.transform.LookAt(player.x, attacker.y, attacker.z) if you want to lookat just 1 axis, the x axis in 2d. then control the looked at object position.

quaternion is not for starting 2d coding, it’s for internal u3d values, 3d rotation variables, and 3d rotations. all eulerangles are accessible as rotations at any time, as they are also return quaternions but very easy to implement.